ESM default via package.json type module in mixed CommonJS application
0 reputation · 10 Feb 2022, 07:57 UTC
Goal is to migrate a small Node.js application from CommonJS to ECMAScript Modules without service interruption. The codebase currently relies on .js files interpreted as CommonJS and on CommonJS-specific loading semantics.
A documented configuration transition is setting package.json "type" to "module", which changes the default interpretation of .js files to ESM. Node.js supports dual-module loading, with ESM able to import CJS, while CJS can only load ESM via dynamic import(). ESM enables top-level await and enforces strict mode by default, and __dirname and __filename are not provided.
The unresolved decision concerns the compatibility boundary for existing consumers and internal files during an incremental change.
Which file naming and package.json configuration preserves import paths for existing CommonJS consumers while allowing new ESM files? How does the static import graph of ESM interact with circular dependencies that were previously tolerated under CommonJS? What is the documented boundary for reconstructing path globals in ESM without breaking mixed loading?