Esm: Question about singleton

Created on 21 Apr 2018  路  2Comments  路  Source: standard-things/esm

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?

question

Most helpful comment

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 :tada:

All 2 comments

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

might be actually both :smiley: 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!

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 :tada:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OmgImAlexis picture OmgImAlexis  路  3Comments

Mensu picture Mensu  路  3Comments

kherock picture kherock  路  3Comments

janusqa picture janusqa  路  3Comments

pvdlg picture pvdlg  路  3Comments