{"record":{"id":"de95ea2714992a0a","repo":"microsoft/TypeScript","slug":"could-not-resolve-js-module-modulename-starti","errorCode":null,"errorMessage":"Could not resolve JS module '${moduleName}' starting at '${initialDir}'. Looked in: ${failedLookupLocations?.join(\", \")}","messagePattern":"Could not resolve JS module '(.+?)' starting at '(.+?)'\\. Looked in: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/compiler/moduleNameResolver.ts","lineNumber":1681,"sourceCode":"    }\r\n    const candidate = normalizePath(combinePaths(baseUrl, moduleName));\r\n    if (state.traceEnabled) {\r\n        trace(state.host, Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, baseUrl, candidate);\r\n    }\r\n    return loader(extensions, candidate, !directoryProbablyExists(getDirectoryPath(candidate), state.host), state);\r\n}\r\n\r\n/**\r\n * Expose resolution logic to allow us to use Node module resolution logic from arbitrary locations.\r\n * No way to do this with `require()`: https://github.com/nodejs/node/issues/5963\r\n * Throws an error if the module can't be resolved.\r\n *\r\n * @internal\r\n */\r\nexport function resolveJSModule(moduleName: string, initialDir: string, host: ModuleResolutionHost): string {\r\n    const { resolvedModule, failedLookupLocations } = tryResolveJSModuleWorker(moduleName, initialDir, host);\r\n    if (!resolvedModule) {\r\n        throw new Error(`Could not resolve JS module '${moduleName}' starting at '${initialDir}'. Looked in: ${failedLookupLocations?.join(\", \")}`);\r\n    }\r\n    return resolvedModule.resolvedFileName;\r\n}\r\n\r\n/** @internal */\r\nexport enum NodeResolutionFeatures {\r\n    None = 0,\r\n    // resolving `#local` names in your own package.json\r\n    Imports = 1 << 1,\r\n    // resolving `your-own-name` from your own package.json\r\n    SelfName = 1 << 2,\r\n    // respecting the `.exports` member of packages' package.json files and its (conditional) mappings of export names\r\n    Exports = 1 << 3,\r\n    // allowing `*` in the LHS of an export to be followed by more content, eg `\"./whatever/*.js\"`\r\n    // not supported in node 12 - https://github.com/nodejs/Release/issues/690\r\n    ExportsPatternTrailers = 1 << 4,\r\n    // allowing `#/` root imports in package.json imports field\r\n    // not supported until mass adoption - https://github.com/nodejs/node/pull/60864\r","sourceCodeStart":1663,"sourceCodeEnd":1699,"githubUrl":"https://github.com/microsoft/TypeScript/blob/b465fdbfe175304d9b977da137b2c178ae1091d3/src/compiler/moduleNameResolver.ts#L1663-L1699","documentation":"Thrown by `ts.resolveJSModule` (in `moduleNameResolver.ts`) when its internal worker cannot resolve `moduleName` from `initialDir`. Unlike `resolveModuleName`, this function is a hard resolver (used to expose Node-style resolution from arbitrary locations) and throws rather than returning an unresolved result. The message lists every lookup location that was tried.","triggerScenarios":"Calling `ts.resolveJSModule(name, dir, host)` where `name` is misspelled, not installed, not reachable on the configured `ModuleResolutionHost`, or where the host's directory/file view is wrong. Also reached when the package exists but is excluded by `exports`/`imports` maps or extension filters.","commonSituations":"Tooling that drives the TS compiler API (language servers, bundlers, custom transforms) calling `resolveJSModule` for a dependency that is not installed; wrong `initialDir` (e.g. a source dir instead of the package root); host that hides `node_modules`; packages without a main entry and no `exports`.","solutions":["Prefer the non-throwing `ts.resolveModuleName(name, containingFile, options, host)` and check `resolvedModule` yourself.","Verify the package is installed under `initialDir`'s `node_modules` and has a resolvable entry.","Check the host's `fileExists`/`directoryExists`/`readDirectory` actually see the candidate paths.","Pass the real `containingFile`/`initialDir` (the file's directory, not a build root)."],"exampleFix":"// before\nconst path = ts.resolveJSModule(\"missing-pkg\", \"./src\", host); // throws\n// after\nconst { resolvedModule } = ts.resolveModuleName(\n  \"missing-pkg\", \"./src/index.ts\", compilerOptions, host,\n);\nif (!resolvedModule) {\n  // handle missing module without a throw\n}","handlingStrategy":"validation","validationCode":"// Non-throwing resolution check before falling back to resolveJSModule:\nconst { resolvedModule } = ts.resolveModuleName(\n  moduleName, containingFile, compilerOptions, host,\n);\nif (!resolvedModule) throw new Error(moduleName + \" not found\");","typeGuard":"function canResolve(name: string, dir: string, host: ts.ModuleResolutionHost, opts: ts.CompilerOptions): boolean {\n  return !!ts.resolveModuleName(name, dir + \"/x.ts\", opts, host).resolvedModule;\n}","tryCatchPattern":"try {\n  const p = ts.resolveJSModule(name, dir, host);\n} catch (e) {\n  // message lists failedLookupLocations; surface to the user as a missing-dep diagnostic\n}","preventionTips":["Use `resolveModuleName` (non-throwing) for user-facing diagnostics; reserve `resolveJSModule` for cases that should hard-fail.","Run resolution from the file's own directory, not a project root.","Ensure `node_modules` and package entries are visible to the host."],"tags":["module-resolution","compiler-api","node-modules","configuration"],"backgroundTag":null,"analyzedSha":"b465fdbfe175304d9b977da137b2c178ae1091d3","analyzedAt":"2026-08-12T05:38:42.698Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}