swc-project/swc · error

index not found

Error message

index not found

What it means

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.

Source

Thrown at crates/swc_ecma_loader/src/resolvers/node.rs:150

        preserve_symlinks: bool,
    ) -> Self {
        Self {
            target_env,
            alias,
            preserve_symlinks,
            ignore_node_modules: true,
        }
    }

    fn wrap(&self, path: Option<PathBuf>) -> Result<FileName, Error> {
        if let Some(path) = path {
            if self.preserve_symlinks {
                return Ok(FileName::Real(path.clean()));
            } else {
                return Ok(FileName::Real(path.canonicalize()?));
            }
        }
        bail!("index not found")
    }

    /// Resolve a path as a file. If `path` refers to a file, it is returned;
    /// otherwise the `path` + each extension is tried.
    fn resolve_as_file(&self, path: &Path) -> Result<Option<PathBuf>, Error> {
        #[cfg(debug_assertions)]
        let _tracing = if cfg!(debug_assertions) {
            Some(
                tracing::span!(
                    Level::TRACE,
                    "resolve_as_file",
                    path = tracing::field::display(path.display())
                )
                .entered(),
            )
        } else {
            None
        };

View on GitHub (pinned to 5176682b65)

Solutions

  1. Add an index file matching a supported extension (index.js, index.ts, index.tsx, index.mjs, index.cjs) to the target directory
  2. Fix the package.json of the target package so its `main` (or exports) points to a file that actually exists
  3. Import an explicit file path (e.g. './foo/bar.js') instead of relying on directory index resolution
  4. If you set up NodeResolver with restrictions (without_node_modules), double-check the module is reachable under those restrictions

Example fix

// before: importing a directory without index
import { helper } from './utils'; // ./utils has only helpers.ts? no index

// after: create ./utils/index.ts re-exporting, or import the file directly
import { helper } from './utils/helpers';
Defensive patterns

Strategy: try-catch

Validate before calling

// Before importing, confirm the directory has an index in a known extension
import { existsSync } from 'node:fs';
const hasIndex = (dir) => ['js','ts','tsx','mjs','cjs','jsx'].some((e) => existsSync(`${dir}/index.${e}`));

Try / catch

match resolver.resolve(base, spec) {
    Ok(file) => file,
    Err(e) if e.to_string().contains("index not found") => resolver.resolve(base, format!("{}/index.js", spec))?,
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/1d55ae0e539bb2e3. Report an issue: GitHub.