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

Question about singleton #373

Closed
ratson opened this issue Apr 21, 2018 · 2 comments
Closed

Question about singleton #373

ratson opened this issue Apr 21, 2018 · 2 comments
Labels

Comments

@ratson
Copy link

ratson commented Apr 21, 2018

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,

npm install
node -r esm main.mjs

While the following output make more sense

singleton.mjs

It prints

singleton.js
singleton.mjs

Is this an expected output for ESM in Node?

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Apr 21, 2018

I am not sure if this is a bug or a feature

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:

singleton.mjs
singleton.mjs

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!

@jdalton
Copy link
Member

jdalton commented Apr 21, 2018

Hi @ratson!

This is by design (at least Node design).
When I run your repo with Node's official --experimental-modules flag I get the same output

> node --experimental-modules main.mjs
singleton.js
singleton.mjs

This is because in .mjs, when looking up extensionless specifiers, it will attempt .mjs first and then .js. While in CommonJS with require() it will attempt .js and not .mjs.

@dnalborczyk

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.

That's a glitch. I shouldn't be allowing that. I'll fix that up. The .mjs file should be locked down. I don't want to allow an inch of usability for it while it's experimental.

Update:

v3.0.23 is released 🎉

@jdalton jdalton closed this as completed Apr 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants