-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
esm: add limited support for --print
#52105
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
this seems similar to the updated |
Does the following work? node --input-type=module --print "import { version } from 'process'; version" The CJS equivalent |
No, as said in the description and can be seen in the tests, this PR adds support for single expression modules only. $ node --input-type=module --print 'await import("node:process").then(({ version }) => version)'
22.0.0
$ node --input-type=module --eval "import { version } from 'process'; console.log(version)"
22.0.0
$ node --input-type=module --print 'process.version'
22.0.0
$ node --version
22.0.0 |
@@ -61,7 +60,7 @@ function getEvalModuleUrl() { | |||
*/ | |||
function evalModuleEntryPoint(source, print) { | |||
if (print) { | |||
throw new ERR_EVAL_ESM_CANNOT_PRINT(); | |||
source = `console.log((${source}))`; |
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.
source = `console.log((${source}))`; | |
source = `console.log(await (async(() => {${source}})()))`; |
will wrapping source with an async iife make multiple expressions and missing await work?
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.
It would force the user to write return
, which would be weird (let’s not forget that using return
at the top-level of a module is not valid syntax)
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.
I feel like if --print
supports multiple expression CommonJS input, if we can’t get it to work likewise for ESM input we should at least error informatively so that the user knows how to fix their input so that it works.
Do you have something in mind regarding what that would look like? |
Could multiple expressions be turned into one by wrapping them in an IIFE? So like “It looks like you wanted to run multiple lines of code; however, |
It's unclear how we could detect if the user is trying to run multiple lines without parsing their code – which maybe is what we should actually do. Note that the async IIFE won't help with |
I think there’s some precedent for this, like maybe in |
The idea is to enable the following use-case:
Obviously it only work for a tiny portion of modules (only modules that have a single expression that is not
import
orexport
), but since there was no support at all before this PR, it's not going to break anyone.