-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
module: allow unrestricted cjs requires #29862
Conversation
3fc9654
to
acab73e
Compare
acab73e
to
1e71621
Compare
//cc @nodejs/modules-active-members |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assuming we get consensus in the modules group, this LGTM
Even beyond the safety guarantees, if I firmly believe that there are no compat concerns for |
@weswigham the assumption for tooling should be the rules of the ESM resolver. This is the well-defined semantics we are placing down. That CommonJS can load a |
Like, without this, static analysis tools like TS are forced to use heuristic syntax based format detection, which was one of the things the field was supposed to avoid - except this is even worse, since it admits the middle ground where modules are valid under both execution environments, rather than formalizing what syntax indicates what format. If a tool is asked to transpile such a module, what is one to do? Assume it's strict mode? Assume it is not? You can choose to dismiss such cases, but our original discussions appeared to weigh those edge cases quite highly, and, if they are being dismissed, more complete alternatives like automatic format detection based on syntax baked into the engine (so there was an authorative specification from which to derive an answer) would both align with existing tooling better and make for a better user experience. |
This isn't an interim solution - this is a walk back of half of |
@weswigham thanks for explaining your case in more detail, I see it presents the ambiguity we were trying to avoid for you. @GeoffreyBooth has suggested we make this an explicit deprecation warning. I could get behind that as an approach that helps guide users to the right approaches. Would that work for you here? |
Make what a deprecation warning? |
My thinking would be that the deprecation warning becomes an error in Node 13/14. The intent of the warning was more to preserve the possibility of I don’t think that |
Except by the rules we've established, And it's not like there aren't other options for people waiting on tools to update - simply not using |
For reference, all that tools need to do to bypass the error (as in, do the “bad” behavior, of loading The issue is really that users add |
Per the last meeting, we are going with a warning as in #29909. |
This implements a follow-up to the issue we had in nodejs/modules#389 with the merge of #29492 and later correction in #29732.
The approach taken is to allow CommonJS modules to
require
any other files, even.js
files in a"type": "module"
boundary to ensure maximum backwards compatibility.require
of.mjs
files is still not permitted though without manually overriddingrequire._extensions['.mjs']
. To retain the module format invariants we specifically then do not inject CJS loads into the ESM loader when they break the expected module format, which is the check implemented here.Technically this allows the module registries to diverge in that we can have the same module path in both the CJS and ESM registries, but meaning different things. Note that only a module not using module syntax or require would cause this though as it would be a failure in either registry otherwise. Because of these syntax restrictions and the top-level main restrictions remaining the same (this only applies from
require()
), the ways this "technical incompleteness" causes real issues is impractically small such that the usability and compatibility benefits seem worth it to me.I expect this will be somewhat controversial and we will need to add this to the modules agenda though.
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes