| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
I've added detection of other global-like CJS variables (exports, __filename, etc.). |
Sorry, something went wrong.
There was a problem hiding this comment.
This is way too long and reminds me of clippy. Something more short and to the point would be better:
| '. It seems you are trying to load a file using `.js` extension ' + | |
| 'inside a folder containing a `package.json` that includes ' + | |
| '`"type": "module"`; you need to use the `.cjs` extension to ' + | |
| 'load the file as a CommonJS module, or add a `package.json` ' + | |
| 'file with ``"type": "commonjs"`.'; | |
| 'CommonJS files can be loaded using the package.json type option or .cjs' |
Sorry, something went wrong.
There was a problem hiding this comment.
Note that it's not much longer than the error message when using named import with CJS:
node/lib/internal/modules/esm/module_job.js
Lines 128 to 134 in 5318e53
In the linked issue, we have a user reporting they got such error without realising they were under a "module" package scope, having a very explicit error message would help I think. (and also everyone loved Clippy anyway, right? right? 🙃). Anyway, if the consensus is to have a shorter message, I'll take your suggestion.
Sorry, something went wrong.
There was a problem hiding this comment.
I was just about to say, we should make this similar to the “named export” error message, which includes the path to the controlling package.json file and things like that. An error message that includes the context to explain what’s going on seems worth the added length. Something like:
your-file-here.js cannot be parsed as an ES module, but it is being treated as an ES module because it has a .js extension and /Users/devsnek/projects/foo/package.json contains "type": "module". To treat it as a CommonJS file, rename it to your-file-here.cjs.
I’m not sure we want to recommend changing the relevant "type" value, since that would likely affect other files that might not be CommonJS. We could also do without the second sentence in my example above, though then we’re leaving the user to Google the error message to figure out what to do to fix this. (The other potential solution that comes to mind is to refactor the offending file into ESM, but that seems a bit much for an error message to suggest.)
Sorry, something went wrong.
There was a problem hiding this comment.
Alternatively, we could link to the docs rather than including the second sentence, and the docs are able to be much more precise without having to be quite so brief.
Sorry, something went wrong.
There was a problem hiding this comment.
I don't think we do link to our docs in any error message so far, maybe there's a good reason for not doing it? If there's none, that seems ideal indeed.
Sorry, something went wrong.
There was a problem hiding this comment.
more than one of these if statements can be true at the same time. we should ensure every combination is a valid sentence or sentences.
Sorry, something went wrong.
There was a problem hiding this comment.
Well I think it is already, isn't it? Do you want me to add a test with all conditions being true at the same time?
Sorry, something went wrong.
There was a problem hiding this comment.
Instead of in ES module scope perhaps we can just say in the ES module ${url}.
Do you want me to add a test with all conditions being true at the same time?
It sounds to me like that is the request here.
Sorry, something went wrong.
There was a problem hiding this comment.
we can just say in the ES module ${url}.
The url is already provided in the first line of the stack trace, is it useful to repeat it here?
Sorry, something went wrong.
There was a problem hiding this comment.
Good poin - if we definitely have an error frame for all of these then we should work to that.
Sorry, something went wrong.
|
@devsnek Are you still -1 on the latest version of this PR? |
Sorry, something went wrong.
There was a problem hiding this comment.
This is a great one, thank you!
Sorry, something went wrong.
There was a problem hiding this comment.
Instead of in ES module scope perhaps we can just say in the ES module ${url}.
Do you want me to add a test with all conditions being true at the same time?
It sounds to me like that is the request here.
Sorry, something went wrong.
There was a problem hiding this comment.
Perhaps - Use "import" instead or
Sorry, something went wrong.
There was a problem hiding this comment.
Is your comment incomplete or? 😅
For reference, the e.message at this point is ReferenceError: require is not defined in ES module scope.
Sorry, something went wrong.
There was a problem hiding this comment.
Was working to ReferenceError: require is not defined in the ES module ${url} per the previous comment.
Sorry, something went wrong.
There was a problem hiding this comment.
The or was supposed to compose with the .cjs extension suggestion, but we could keep that at the end too.
Sorry, something went wrong.
There was a problem hiding this comment.
Oh I see! Note that the second part is printed out only if the module is .js, we need to take into account the case where the module is using a different extension or is a data: URL.
Sorry, something went wrong.
|
I came across something really annoying yesterday where I had to use e.message.split('\n')[0] to achieve proper presentation because node was inserting a bunch of garbage into the message. I think we should hold off on this or put the text somewhere else. Maybe we can attach a detail property or something? |
Sorry, something went wrong.
|
@devsnek that's a good point, but I don't think now is the time rework long error messages in Node.js that provide context. Rather that should ideally be a separate PR process. |
Sorry, something went wrong.
|
@guybedford sure, I'm saying at the very least, hold off on landing this until we figure that out. I'm personally willing to put in that effort (this weekend perhaps). |
Sorry, something went wrong.
|
@devsnek that could work, can we at least set a rough time frame on such a hold to ensure this doesn't go stale? |
Sorry, something went wrong.
|
We could use the cause property (without waiting for the constructors to support it) since that’ll be what’s standard |
Sorry, something went wrong.
|
@devsnek would it be OK to land this now?
BTW if you want/need help on that, please reach out. I agree with Jordan, adding a cause property seems like a plan. |
Sorry, something went wrong.
|
I'm planning to land this in a few days if no one objects. |
Sorry, something went wrong.
Sorry, something went wrong.
Sorry, something went wrong.
Fixes: nodejs#33741 PR-URL: nodejs#37852 Reviewed-By: Guy Bedford <guybedford@gmail.com>
| Back | FazBrowse Home | New Git URL |
Error message on Node.js v15.x:
ReferenceError: require is not defined at data:text/javascript,require;:1:1 at ModuleJob.run (node:internal/modules/esm/module_job:154:23) at async Loader.import (node:internal/modules/esm/loader:166:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15) at async waitForActual (node:assert:736:5) at async Function.rejects (node:assert:845:25)Error message with this PR:
ReferenceError: require is not defined in ES module scope, you can use import instead at data:text/javascript,require;:1:1 at ModuleJob.run (node:internal/modules/esm/module_job:155:25) at async Loader.import (node:internal/modules/esm/loader:166:24) at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15) at async waitForActual (node:assert:736:5) at async Function.rejects (node:assert:845:25)@nodejs/modules I'm not sure we should keep the .cjs advice because it doesn't always apply – sometimes there are no files (E.G.: data: URL), and this behaviour could be overridden by a custom loader. On the other hand, for a user stumbling over this issue, the suggestion could be helpful. wdyt?
Fixes: #33741
In the issue, it's also been discussed to add a message explaining why the file was parsed as ESM rather than CJS, but I couldn't find how to do that.
EDIT: When the module is a file with .js extension, the following error message is shown: