-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
Combine ERR_REQUIRE_ESM with warning #30599
Comments
I'm gonna take this one. |
@juanarbol that would be really great - just let me know if I can help. |
@guybedford sure! I'll ask you for some help if needed (I think i'll need it) |
So, we still needing to throws the exception, but now, the warning should contain error ( |
@juamedgod yes the error should contain in the message the descriptive contents of what is currently in the warning. |
Jm, I'm misunderstanding something. So, we need to emit the current warning as is, but with the message of In a most short way; the warning message emitted, should be the current message concatenated with ERR_REQUIRE_ESM message, and the message of the trowned ERR_REQUIRE_ESM should the same of the emitted warning. Am I right? |
So at the moment we provide both a warning and an error message. The error message should continue to be thrown, but the useful information from the warning can be folded into the error message itself. This way it will still be shown to users, but libraries can wrap the error and swallow it if they need, whereas the warning cannot be silenced without disabling all warnings in Node.js. |
This is what I've done: try {
const a = require('./mod/file2.js')
console.log(a)
} catch (err) {
console.log('\nError message: ')
console.log(err.message)
} (node:91392) Warning: require() of ES modules is not supported.
require() of /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js from /Users/juanjose/Documents/GitHub/Node/node/file.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename file2.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/juanjose/Documents/GitHub/Node/node/mod/package.json.
Must use import to load ES Module: /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js
Error message:
Must use import to load ES Module: /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js
require() of ES modules is not supported.
require() of /Users/juanjose/Documents/GitHub/Node/node/mod/file2.js from /Users/juanjose/Documents/GitHub/Node/node/file.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename file2.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/juanjose/Documents/GitHub/Node/node/mod/package.json. Is this the expected behaviour? |
PR-URL: #30694 Fixes: #30599 Reviewed-By: Guy Bedford <[email protected]>
PR-URL: #30694 Fixes: #30599 Reviewed-By: Guy Bedford <[email protected]>
PR-URL: #30694 Fixes: #30599 Reviewed-By: Guy Bedford <[email protected]>
Now that modules are unflagged, the warning for ERR_REQUIRE_ESM when loading a
.js
file that would be interpreted as a module is being thrown along with the warning message in https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L1193.This hinders approaches that want to use this error to explicitly check for support.
Ideally the warning can just be combined with the error message.
The text was updated successfully, but these errors were encountered: