Code Monkey home page Code Monkey logo

esm-test-pkg's Introduction

ESM Package Testing

Issue

We have a package dependency with both package.json:exports and a "type": "module" declaration. The issue is -- can CJS work at all?

Conclusion

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.

Usage

ESM

Try the ESM version first and verify it works...

$ node index.mjs
{ msg: 'ESM' }

CJS

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

Notes

Changing CJS suffix would work

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"
     }
   }
 }

esm-test-pkg's People

Contributors

ryan-roemer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.