swc-project/swc · error · Error
Error occurred while loading config file at ${config}: ${e}
Error message
Error occurred while loading config file at ${config}: ${e} What it means
compileBundleOptions() backs swc.bundle() and the spack CLI: it loads your config by calling require() on the path you passed (or a default) and merges it with inline options. Any failure inside that require — file not found, a syntax error, TypeScript/ESM syntax require() cannot evaluate, or an exception thrown while the config module executes — is caught and rethrown prefixed with the config path, with the original error appended after the colon. The prefix tells you the problem is the config file, not the bundler.
Source
Thrown at packages/core/src/spack.ts:39
configFromFile = (configFromFile as any).default;
}
if (Array.isArray(configFromFile)) {
if (Array.isArray(f)) {
return [...configFromFile, ...f];
}
if (typeof f !== "string") {
configFromFile.push(f);
}
return configFromFile;
}
return {
...configFromFile,
...(typeof config === "string" ? {} : config),
};
} catch (e) {
if (typeof f === "string") {
throw new Error(
`Error occurred while loading config file at ${config}: ${e}`
);
}
return f;
}
}
/**
* Usage: In `spack.config.js` / `spack.config.ts`, you can utilize type annotations (to get autocompletions) like
*
* ```ts
* import { config } from '@swc/core/spack';
*
* export default config({
* name: 'web',
* });
* ```
*View on GitHub (pinned to 5176682b65)
Solutions
- Read the text after the last ': ' — it carries the underlying require() error (MODULE_NOT_FOUND, SyntaxError, ...) which names the real cause.
- Make the config CommonJS-loadable: use spack.config.js/.cjs with module.exports (or compile the .ts file to .js first); for ESM packages use a .cjs config.
- Verify the path exists relative to the process cwd; prefer absolute paths.
- If you don't need a file, pass a BundleOptions object directly to swc.bundle() to bypass require() entirely.
Example fix
// before — spack.config.ts cannot be require()d by default
await swc.bundle('./spack.config.ts');
// Error occurred while loading config file at ./spack.config.ts: ...
// after — either compile to .js first, or pass options inline
await swc.bundle({
mode: 'production',
target: 'browser',
entry: { web: './src/a.ts' },
output: { path: './dist' },
}); Defensive patterns
Strategy: validation
Validate before calling
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
export function assertSpackConfigLoadable(configPath: string): void {
const abs = resolve(configPath);
if (!existsSync(abs)) throw new Error(`spack config not found: ${abs}`);
if (/\.(ts|mts|cts)$/.test(abs)) {
throw new Error(
'spack loads configs via require(); use a .js/.cjs config or compile the .ts file'
);
}
const src = readFileSync(abs, 'utf8');
if (!/module\.exports|exports\./.test(src) && /export\s+default/.test(src)) {
throw new Error(`${abs} uses ESM exports but is loaded with require()`);
}
} Try / catch
try {
await swc.bundle(configPath);
} catch (e) {
const msg = (e as Error).message;
if (msg.startsWith('Error occurred while loading config file')) {
// the text after the last ': ' is the underlying require() failure
throw new Error(
`spack config failed to load: ${msg.slice(msg.lastIndexOf(': ') + 2)}`
);
}
throw e;
} Prevention
- Keep spack configs as plain .js/.cjs CommonJS unless your toolchain registers a require loader for TS.
- Resolve config paths against a known root (path.resolve(__dirname, ...)) instead of relying on cwd.
- Smoke-load the config in CI (node -e "require('./spack.config.js')") to catch syntax/dependency errors early.
When it happens
Trigger: swc.bundle('./spack.config.js') (or the spack CLI with a config path) where the file is missing, has a syntax or runtime error, is spack.config.ts loaded by plain require() with no TS loader, is ESM (package 'type': 'module' with export default) required from CJS, or imports a package that fails to resolve. Also swc.bundle() with no argument where nothing is requireable — the message then interpolates `config` and prints 'at undefined: ...'.
Common situations: Teams renaming the config to .ts for autocompletion and hitting require() failures; ESM-migrated packages; running the CLI from the wrong cwd so a relative config path misses; configs importing env-dependent modules that throw in production.
Related errors
- {} is not a valid expression
- failed to parse jsx option {}: '{}' is not an expression
- determine_export_name({:?})
- emitting decorator metadata while using new proposal
- `env` and `jsc.target` cannot be used together
AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17).
Data as JSON: /api/errors/13a3827101e345de.
Report an issue: GitHub.