We have a package dependency with both package.json:exports
and a "type": "module"
declaration. The issue is -- can CJS work at all?
There is no way to require
files from a package.json
that has "type": "module"
declared, even if it has package.json:exports.*.require
entries.
Try the ESM version first and verify it works...
$ node index.mjs
{ msg: 'ESM' }
Now try the CJS version and get a failure:
$ node index.js
# Expected: { msg: 'CJS' }
/Users/rye/scm/fmd/esm-test-pkg/index.js:2
const msg = require("my-pkg");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/rye/scm/fmd/esm-test-pkg/node_modules/my-pkg/cjs.js from /Users/rye/scm/fmd/esm-test-pkg/index.js not supported.
cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/rye/scm/fmd/esm-test-pkg/node_modules/my-pkg/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
at Object.<anonymous> (/Users/rye/scm/fmd/esm-test-pkg/index.js:2:13) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v18.7.0
Changing node_modules/my-pkg/cjs.js
to node_modules/my-pkg/cjs.cjs
with the .cjs
suffix, would cause the program to work:
diff --git a/node_modules/my-pkg/cjs.js b/node_modules/my-pkg/cjs.cjs
similarity index 100%
rename from node_modules/my-pkg/cjs.js
rename to node_modules/my-pkg/cjs.cjs
diff --git a/node_modules/my-pkg/package.json b/node_modules/my-pkg/package.json
index bb7f270..af7610e 100644
--- a/node_modules/my-pkg/package.json
+++ b/node_modules/my-pkg/package.json
@@ -9,7 +9,7 @@
"exports": {
".": {
"import": "./esm.js",
- "require": "./cjs.js"
+ "require": "./cjs.cjs"
}
}
}