-
Notifications
You must be signed in to change notification settings - Fork 147
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
Question about singleton #373
Comments
might be actually both 😃 depending on how you look at it. a lot of things regarding module loading in node are still in flux, but as it stands, you can't require 'mjs' file extensions in node itself. you can try it out yourself: // consumer.js
require('./singleton.mjs') and run: node --experimental-modules main throws: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/bug-esm-singleton/singleton.mjs the part which might contribute to more confusion is that the esm loader allows requiring mjs file extensions, and therefore es6 modules, and you end up with 2 singletons. // consumer.js
require('./singleton.mjs') and run: node -r esm main prints:
there's a certain order how file-extensions are resolved and rules what they can contain. e.g. mjs files can't contain cjs modules (require/module.export). cjs and es6 modules also create their own scope, es6 modules use bindings... supporting js file extensions for ES6 modules is a feature of the esm-loader, and so far, not supported by the experimental loader from node itself. though there's some light on the horizon with a working group for node modules now, and hopefully things will progress in the right direction. Hopefully 'mjs' will be an experimental thing of the past and quickly forgotten! |
Hi @ratson! This is by design (at least Node design). > node --experimental-modules main.mjs
singleton.js
singleton.mjs This is because in
That's a glitch. I shouldn't be allowing that. I'll fix that up. The Update: v3.0.23 is released 🎉 |
I am not sure if this is a bug or a feature, requiring
.mjs
and.js
is possible.I have created a repo to demo the problem, https://github.com/ratson/bug-esm-singleton
It can be run with the following commands,
While the following output make more sense
It prints
Is this an expected output for ESM in Node?
The text was updated successfully, but these errors were encountered: