Atlaslog
Atlaslog / Field Notes / Build & deployment
Field note · Build & deployment

Importing a CommonJS package breaks the production build

Quick answer

The dev server runs fine, but `npm run build` aborts with a SyntaxError saying the module provides no export named 'default'. The package is UMD/CJS only, so the bundler's ESM interop and dependency pre-bundling decide whether a default export exists.

Last verified 2026-11-03 · Multiple versions · Cite this page
Applies to: Vite 5.4 / webpack 5 / proj4 2.12
Symptom
Checklist
  1. Read the package's package.json: a `main` entry with no `module` or `exports` field means CJS
  2. Run `node -e "const p=require('proj4'); console.log(typeof p, Object.keys(p).slice(0,5));"` and compare with the dynamic `import('proj4')` shape
  3. Search the build log for `commonjs` or `optimizeDeps` mentions
  4. Try a namespace import and read `.default` to confirm the interop assumption
  5. Rebuild after adding the package to `optimizeDeps.include`
Fix

Add `proj4` to `optimizeDeps.include` and set `build.commonjsOptions.transformMixedEsModules: true`, then use `import proj4 from 'proj4'`; when interop still fails, fall back to `import * as proj4Module from 'proj4'` plus `const proj4 = proj4Module.default || proj4Module`.

Code
error during build:
SyntaxError: The requested module '/node_modules/.vite/deps/proj4.js?v=9a1c3f2e'
does not provide an export named 'default'
WARNING in ./src/geo/transform.js
"export 'default' (imported as 'proj4') was not found in 'proj4'
Watch out
All 120 field notesChinese originalRunnable examples
English edition written for Atlaslog from the original Chinese field note. Code is unchanged and runs as-is.
Cite this page

Atlaslog (2026). Importing a CommonJS package breaks the production build. https://www.jingweizhi.com/en/cookbook/esm-cjs-mixed.html (accessed 2026-11-03).