swc-project/swc · error
file not found: {}
Error message
file not found: {} What it means
NodeResolver::resolve_as_file bails with this error after every candidate for a path fails. Candidates are: the exact path if it already has an extension, `<path>.js`, the raw path itself, then every extension in EXTENSIONS, plus TypeScript-style substitution (.js/.jsx -> .ts/.tsx, .mjs -> .mts, .cjs -> .cts). So the specifier looked like a file but no file with any recognized extension exists at that location.
Source
Thrown at crates/swc_ecma_loader/src/resolvers/node.rs:230
// ".tsx" even if the original extension was ".jsx".
"js" => &["ts", "tsx"],
"jsx" => &["ts", "tsx"],
"mjs" => &["mts"],
"cjs" => &["cts"],
_ => &[],
};
for ext in extensions {
ext_path.set_extension(ext);
if ext_path.is_file() {
return Ok(Some(ext_path));
}
}
}
}
bail!("file not found: {}", path.display())
}
/// Resolve a path as a directory, using the "main" key from a package.json
/// file if it exists, or resolving to the index.EXT file if it exists.
fn resolve_as_directory(
&self,
path: &Path,
allow_package_entry: bool,
) -> Result<Option<PathBuf>, Error> {
#[cfg(debug_assertions)]
let _tracing = if cfg!(debug_assertions) {
Some(
tracing::span!(
Level::TRACE,
"resolve_as_directory",
path = tracing::field::display(path.display())
)
.entered(),View on GitHub (pinned to 5176682b65)
Solutions
- Verify the exact file exists on disk (watch out for case sensitivity and stale builds); fix the import path or restore the file
- For '.js' imports in TypeScript projects, confirm the corresponding '.ts'/'.tsx' file exists (the resolver only substitutes those)
- If you need custom extensions, wire a custom resolver/transform in the loader pipeline instead of relying on NodeResolver defaults
- Run git submodule update --init --recursive if the path lives in a submodule not checked out
Example fix
// before
import { x } from './util/helper'; // no helper.ts on disk
// after (file is helper/index.ts)
import { x } from './util/helper/index';
// or create the missing ./util/helper.ts Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check that some candidate file exists for a relative specifier
import { existsSync } from 'node:fs';
const EXTS = ['', '.js', '.ts', '.tsx', '.mjs', '.cjs', '.jsx', '.mts', '.cts'];
const resolvable = (p) => EXTS.some((e) => existsSync(p + e)); Try / catch
try {
const resolved = resolver.resolve(base, spec);
} catch (e) {
if (e.message.startsWith('file not found')) throw new Error(`Cannot resolve '${spec}' from ${base}: check the path, extension, and case`);
throw e;
} Prevention
- Run typecheck (tsc --noEmit) before bundling to catch broken relative imports early
- Watch for case-sensitivity issues when builds move from macOS/Windows to Linux
- Keep file renames and import updates in the same commit; lint with import/no-unresolved
When it happens
Trigger: An import (or require) of a relative/absolute specifier that does not exist on disk in any of the resolver's supported extensions, e.g. './missing-module', './config.json5' when the file is absent, or './foo.js' when only a sibling with an extension outside the substitution table exists.
Common situations: Typos in import paths after a rename/move refactor; case-sensitivity mismatches working on Windows then building on Linux; importing files with unsupported extensions (e.g. '.vue', '.svelte') through NodeResolver without a custom extension handling or plugin; missing files in a shallow git checkout/submodule.
Related errors
- index not found
- node-resolver supports only files
- `{module_specifier}` matched `{prefix}` (from tsconfig.paths
- The requested const_module `{:?}` does not provide an export
- The const_module namespace `{sym}` cannot be used without me
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/bb4435a37cbc2dac.
Report an issue: GitHub.