Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module does not work with require() #1

Closed
joepie91 opened this issue Aug 21, 2021 · 2 comments
Closed

Module does not work with require() #1

joepie91 opened this issue Aug 21, 2021 · 2 comments

Comments

@joepie91
Copy link

Requiring this module like so:

const bigVarint = require("big-varint");

... produces the following error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/sven/projects/zapdb-kv/node_modules/big-varint/lib/index.js
require() of ES modules is not supported.
require() of /home/sven/projects/zapdb-kv/node_modules/big-varint/lib/index.js from /home/sven/projects/zapdb-kv/src/storage-encoder/encode.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/sven/projects/zapdb-kv/node_modules/big-varint/package.json.

Unfortunately, while it's possible to import CJS modules from ESM code, the other way around is not possible (or at least not when using "type": "module"), so for maximum compatibility a library should use CJS-compatible exports.

@joeltg
Copy link
Owner

joeltg commented Aug 21, 2021

so for maximum compatibility a library should use CJS-compatible exports

I understand that ESM modules are less “compatible”, but they’re clearly the best way to build and distribute libraries going forward. See this explanation by Sindre Sorhus.

@joeltg joeltg closed this as completed Aug 21, 2021
@joepie91
Copy link
Author

joepie91 commented Aug 21, 2021

I am aware of their explanations; unfortunately, they are incorrect, as ES modules are in many ways worse, and do not fully replace the functionality of CommonJS modules. I have attempted to bring this up with them, unfortunately without result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants