{"record":{"id":"13a3827101e345de","repo":"swc-project/swc","slug":"error-occurred-while-loading-config-file-at-conf","errorCode":null,"errorMessage":"Error occurred while loading config file at ${config}: ${e}","messagePattern":"Error occurred while loading config file at (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/spack.ts","lineNumber":39,"sourceCode":"            configFromFile = (configFromFile as any).default;\n        }\n        if (Array.isArray(configFromFile)) {\n            if (Array.isArray(f)) {\n                return [...configFromFile, ...f];\n            }\n            if (typeof f !== \"string\") {\n                configFromFile.push(f);\n            }\n            return configFromFile;\n        }\n\n        return {\n            ...configFromFile,\n            ...(typeof config === \"string\" ? {} : config),\n        };\n    } catch (e) {\n        if (typeof f === \"string\") {\n            throw new Error(\n                `Error occurred while loading config file at ${config}: ${e}`\n            );\n        }\n        return f;\n    }\n}\n\n/**\n * Usage: In `spack.config.js` / `spack.config.ts`, you can utilize type annotations (to get autocompletions) like\n *\n * ```ts\n * import { config } from '@swc/core/spack';\n *\n * export default config({\n *      name: 'web',\n * });\n * ```\n *","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/packages/core/src/spack.ts#L21-L57","documentation":"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.","triggerScenarios":"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: ...'.","commonSituations":"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.","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."],"exampleFix":"// before — spack.config.ts cannot be require()d by default\nawait swc.bundle('./spack.config.ts');\n// Error occurred while loading config file at ./spack.config.ts: ...\n\n// after — either compile to .js first, or pass options inline\nawait swc.bundle({\n  mode: 'production',\n  target: 'browser',\n  entry: { web: './src/a.ts' },\n  output: { path: './dist' },\n});","handlingStrategy":"validation","validationCode":"import { existsSync, readFileSync } from 'fs';\nimport { resolve } from 'path';\n\nexport function assertSpackConfigLoadable(configPath: string): void {\n  const abs = resolve(configPath);\n  if (!existsSync(abs)) throw new Error(`spack config not found: ${abs}`);\n  if (/\\.(ts|mts|cts)$/.test(abs)) {\n    throw new Error(\n      'spack loads configs via require(); use a .js/.cjs config or compile the .ts file'\n    );\n  }\n  const src = readFileSync(abs, 'utf8');\n  if (!/module\\.exports|exports\\./.test(src) && /export\\s+default/.test(src)) {\n    throw new Error(`${abs} uses ESM exports but is loaded with require()`);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await swc.bundle(configPath);\n} catch (e) {\n  const msg = (e as Error).message;\n  if (msg.startsWith('Error occurred while loading config file')) {\n    // the text after the last ': ' is the underlying require() failure\n    throw new Error(\n      `spack config failed to load: ${msg.slice(msg.lastIndexOf(': ') + 2)}`\n    );\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["swc","spack","config","require","commonjs"],"backgroundTag":"config-file-load-failed","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"}