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

Please don't prevent ES modules from being used on older Node.js versions #4297

Closed
snoack opened this issue May 21, 2020 · 6 comments · Fixed by #4299
Closed

Please don't prevent ES modules from being used on older Node.js versions #4297

snoack opened this issue May 21, 2020 · 6 comments · Fixed by #4299

Comments

@snoack
Copy link
Contributor

snoack commented May 21, 2020

Is your feature request related to a problem or a nice-to-have?? Please describe.
We still support Node.js 10.19.0 and would like to use ES modules. But Mocha refuses to use import() on versions older than Node.js 12.11. However, if I relax that check, Mocha just works fine with ES modules on Node.js 10.19.0 (when using the --experimental-modules flag).

Describe the solution you'd like
It would be great if the version check could be relaxed so that ES modules can be used on Node 10.19.

Or maybe that version check could be removed altogether. After all is import() only used if the file extension is .mjs or if require() fails anyway.

@snoack snoack added the type: feature enhancement proposal label May 21, 2020
@boneskull boneskull added unconfirmed-bug and removed type: feature enhancement proposal labels May 21, 2020
@boneskull
Copy link
Member

I'm calling this a bug. Because pre-v12.11.0, Node.js' implementation had some differences, so it's more of a "use at your own risk" thing. @giltayar are you able to explain and/or address this?

@snoack
Copy link
Contributor Author

snoack commented May 21, 2020

FWIW, I filed this as a feature request rather than as a bug since it works as documented. Still it would be nice to lift this limitation. Removing the version check and updating the documentation to say "use at your own risk on versions of Node.js before X.X" sounds like a great idea.

@giltayar
Copy link
Contributor

giltayar commented May 22, 2020

Node v10 ESM support is the "old support" (before the big changes that made ESM what it is today (e.g. before "type": "module")). So I didn't even try to see if it works.

@boneskull, if you think it's important, I can do a PR to lift that restriction, see that it works, and add a "try at your own risk" note in the docs, as @snoack suggested.

But @snoack, be aware that ESM in Node v12-14 is not compatible with ESM in Node v10, and that all ES modules in NPM currently assume Node v13 and above, and assume that they have things like "type": "module", conditional exports, and others. I would strongly advise to wait till you work on Node v13-14 till you move to ESM. Another problem with ESM in Node v10, is that it can only work with .mjs extension, and I have found that lots of tools don't support .mjs well, and so most of my ESM packages use type: module in their package.json to enable .js ESM files.

@snoack
Copy link
Contributor Author

snoack commented May 22, 2020

For reference, this is what I am doing. FWIW, I found a workaround to bypass your version check without patching Mocha. Anyway, I tested that code on Node.js 10.19.0, 10.20.1, 12.16.2 and 13.13.0 with the same result.

Maybe there are some edge cases that are not supported on Node.js 10.x, but as far as Mocha is interacting with ESM, as well as any use case I could come up with in our own code (not including conditional exports), everything works just fine across these versions.

Finally, in case you wonder why Node.js 10.x matters, it's the version that comes with most Linux distributions, and it perfectly does the job for what we need, i.e. build and development tooling, and testing. I rather don't increase the burden for our developers and contributors by requiring them to install software outside of their package manger if not absolutely necessary. Let alone that some distributions are packaging our software, and therefore we have to remain compatible with them.

@giltayar
Copy link
Contributor

@snoack How can I say no to somebody that wants to use ESM? 😀

Here you go: #4299

Now it's up to the the maintainers to decide if they want unofficial support for ESM on Node v10. If they do, they can accept the PR that relaxes the check and enables Mocha ESM support on Node v10.

@snoack
Copy link
Contributor Author

snoack commented Jun 3, 2020

For reference, this is how I ended up detecting ESM support in Gulp. No hard-coded versions, but feature detection.

Gulp is backwards-compatible all the way back to Node.js 0.10. However, if you don't care about versions older than Node.js 10, you don't even need that. Starting with Node.js 10.15.3 import() just returns a rejected promise if experimental modules are not enabled.

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

Successfully merging a pull request may close this issue.

3 participants