{"record":{"id":"1d55ae0e539bb2e3","repo":"swc-project/swc","slug":"index-not-found","errorCode":null,"errorMessage":"index not found","messagePattern":"index not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_ecma_loader/src/resolvers/node.rs","lineNumber":150,"sourceCode":"        preserve_symlinks: bool,\n    ) -> Self {\n        Self {\n            target_env,\n            alias,\n            preserve_symlinks,\n            ignore_node_modules: true,\n        }\n    }\n\n    fn wrap(&self, path: Option<PathBuf>) -> Result<FileName, Error> {\n        if let Some(path) = path {\n            if self.preserve_symlinks {\n                return Ok(FileName::Real(path.clean()));\n            } else {\n                return Ok(FileName::Real(path.canonicalize()?));\n            }\n        }\n        bail!(\"index not found\")\n    }\n\n    /// Resolve a path as a file. If `path` refers to a file, it is returned;\n    /// otherwise the `path` + each extension is tried.\n    fn resolve_as_file(&self, path: &Path) -> Result<Option<PathBuf>, Error> {\n        #[cfg(debug_assertions)]\n        let _tracing = if cfg!(debug_assertions) {\n            Some(\n                tracing::span!(\n                    Level::TRACE,\n                    \"resolve_as_file\",\n                    path = tracing::field::display(path.display())\n                )\n                .entered(),\n            )\n        } else {\n            None\n        };","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_ecma_loader/src/resolvers/node.rs#L132-L168","documentation":"In swc_ecma_loader's NodeResolver, directory resolution ends by calling wrap(path). wrap() converts the found PathBuf into a FileName; when resolution produced no path at all (None), it bails with 'index not found'. This happens when a package/directory import had no usable package.json entry and no index.<EXT> file inside it, mirroring Node's classic directory resolution failing.","triggerScenarios":"Importing a specifier that resolves to a directory which contains neither a package.json with a resolvable main/exports entry nor an index.js/index.ts/index.tsx/index.mjs/index.cjs/index.jsx file; using NodeResolver::without_node_modules while relying on package index resolution.","commonSituations":"A dependency published without package.json main pointing at a real file (or pointing to a missing file); importing an empty scaffolding directory; monorepo path aliases that land on a folder without an index file; .mjs/.cjs-only packages where the resolver's extension list misses the entry.","solutions":["Add an index file matching a supported extension (index.js, index.ts, index.tsx, index.mjs, index.cjs) to the target directory","Fix the package.json of the target package so its `main` (or exports) points to a file that actually exists","Import an explicit file path (e.g. './foo/bar.js') instead of relying on directory index resolution","If you set up NodeResolver with restrictions (without_node_modules), double-check the module is reachable under those restrictions"],"exampleFix":"// before: importing a directory without index\nimport { helper } from './utils'; // ./utils has only helpers.ts? no index\n\n// after: create ./utils/index.ts re-exporting, or import the file directly\nimport { helper } from './utils/helpers';","handlingStrategy":"try-catch","validationCode":"// Before importing, confirm the directory has an index in a known extension\nimport { existsSync } from 'node:fs';\nconst hasIndex = (dir) => ['js','ts','tsx','mjs','cjs','jsx'].some((e) => existsSync(`${dir}/index.${e}`));","typeGuard":null,"tryCatchPattern":"match resolver.resolve(base, spec) {\n    Ok(file) => file,\n    Err(e) if e.to_string().contains(\"index not found\") => resolver.resolve(base, format!(\"{}/index.js\", spec))?,\n    Err(e) => return Err(e),\n}","preventionTips":["Add index.<ext> files to directories you import as packages","Keep package.json `main` pointing at files that exist; verify after renames","Prefer explicit file imports over bare-directory imports for entry points"],"tags":["module-resolution","node-resolver","index-file","swc-ecma-loader","import"],"backgroundTag":"module-not-found","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}