-
Notifications
You must be signed in to change notification settings - Fork 147
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module / require() of ES modules is not supported #868
Comments
We've just hit this after pulling the latest |
What a mess, on node This happens because I have in node_modules both:
@jdalton I guess ESM is transpiling both to EDIT: just reverted to node (node:7983) Warning: require() of ES modules is not supported. |
Ran into this problem, but for us, we tracked it down to @babel/runtime and this commit. Reverting to @babel/runtime@7.7.2 resolved the issue for us. It seems that the node v12 revamp of the native esm implementation to match node v13 triggered this, but the implications of this on this package (standard-things/esm) are not clear to me. |
This is in fact the same bug as #855. It's an warning in Node 12.15 but becomes an error in Node 12.16. |
Which version of esm are you getting this error with? |
We are using |
Note this error is thrown by the If someone wants to work on a PR to fix const module = require('module');
module.Module._extensions['.js'] = function(module, filename) {
const content = fs.readFileSync(filename, 'utf8');
module._compile(content, filename);
}; |
Would it be possible to switch the behaviors of importing with .js to either .cjs or .mjs depending on the type: module declaration in each package’s package.json? |
Thanks. Downgrade @babel/runtime to 7.7.2 fixed it for me. |
I was able to get this working using the extension handler override solution above (thanks @guybedford!! 🙏) with a tweak to const module = require('module');
const fs = require('fs');
module.Module._extensions['.js'] = function (module, filename) {
const contents = fs.readFileSync(filename, 'utf8');
module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
}; or if you’re willing to sacrifice code clarity for something more condensed: require('module').Module._extensions['.js'] = function (module, filename) {
module._compile(require('fs').readFileSync(filename, 'utf8'), filename);
}; For purists, it’s worth mentioning that this can be used with ES module syntax too. import { readFileSync } from 'fs';
import module from 'module';
module.Module._extensions['.js'] = function (module, filename) {
module._compile(readFileSync(filename, 'utf8'), filename);
}; Whatever you use, make sure to included it as early as possible in your app. (In my case it was a Vue/Nuxt app so I put it at the top of |
Thanks @guybedford and @seanCodes for your monkey patches. Unfortunately, in my case it just results in
albeit I assigend the loader function to both |
Oh, it looks like Node 14.13.0+ supports named imports from common-js modules!: https://simonplend.com/node-js-now-supports-named-imports-from-commonjs-modules-but-what-does-that-mean Well, some of them anyway. (the blog post linked shows how it only works for some packages -- ones where cjs-module-lexer is able to detect the export names through static analysis) There may not be a need for these sorts of workarounds soon then. (I'll have to see, after using Node 14.13.0+ for a while) |
Thanks for the idea. However I doubt that it's a problem with the That said, I don't understand what "without" means in the quote. Are esm files limited unless support is enabled in the options, or are they limited in that none of the options are supported? Then again, none of both interpretations would explain why it works on node v8.17.0. |
Concluding the discussion, this snippet shall work https://gist.github.com/diauweb/f9f7be271ecc7bc0aecaf3dc891e18d2 |
Thanks, I'll test it asap. Meanwhile I also found the fix-esm module. |
I think I understand the use case now. Your snippet and the |
I made a reproduction case: https://stackblitz.com/edit/node-fyfxhz?file=main.js
importing |
I had a hell of a time with all this. I'm just posting my solution repo, in hopes it helps someone else. This imports an ESM dependency (ora) into TypeScript without using babel. |
Oh snap 🤣 So happy to have come across your fork here! TY! My issue was.. I needed Your boilerplate made all my problems disapear 👊💯 |
I am constantly switching node versions (using nvm) to work on different projects. This was the root of my issue and needed to be on v14.0 |
const random = require('random'); Error [ERR_REQUIRE_ESM]: require() of ES Module F:\All Web Development World\Github-bot\node_modules\random\dist\random.module.js from F:\All Web Development |
Whoever wants to use both require and import, you only have to add these two lines at the top of your root project (index.js)
and in package.json, you have to add
Hope it works for you |
@adembacajdev Glad that it works for you. The problem is that declaring your package as type: module comes at a cost that lots of people don't want to pay. |
I have suddenly started getting the following error:
This doesn't make any sense, as I am not using
require()
anywhere at all.It seems to be happening with Node.js > v12.12.0, including all v13.
Strangely, it seems that when I get this in my local environment, if I switch to 12.12.0 (using nvm), run tests, then switch back to later versions of Node, then all tests pass successfully (up to and including with v13.8.0).
However, I can't do this for the CI tests: travis-ci.org/chrisveness/geodesy/builds/649986393 (repo: github.com/chrisveness/geodesy.
Is there any way I can git CI tests to run successfully, as I can get local tests to run by switching versions of Node?
Thanks
The text was updated successfully, but these errors were encountered: