node
node copied to clipboard
module: do not set CJS variables for Worker eval
There are no local CJS variables in the Worker eval context β it's all global variables, just like in the --eval context.
Review requested:
- [ ] @nodejs/loaders
I think the same bug applies to regular --eval, too, doesnβt it?
node --eval "let __filename,__dirname,require,module,exports;this.a" # Clean exit
node --experimental-detect-module --eval "let __filename,__dirname,require,module,exports;this.a"
file:///Users/geoffrey/Sites/node/[eval1]:1
let __filename,__dirname,require,module,exports;this.a
^
TypeError: Cannot read properties of undefined (reading 'a')
at file:///Users/geoffrey/Sites/node/[eval1]:1:54
at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
at async ModuleLoader.eval (node:internal/modules/esm/loader:218:24)
at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5)
Node.js v22.2.0
I feel like we should fix it for --eval as well and add a corresponding test.
Probably a separate PR, but could detect-module be a way to resolve https://github.com/nodejs/node/issues/30682?
IMO that could work. As soon as detect-module will also be the default (i.e. unflagged) it should work seamlessly, excepr the performance warning that should be suppressed in case of eval.
could detect-module be a way to resolve #30682?
I mean, it's already the case, no? When you set --experimental-detect-module, passing a string containing JS code that's parsable as ESM and not parsable as CJS to the Worker constructor does eval it as ESM.
CI: https://ci.nodejs.org/job/node-test-pull-request/59373/
CI: https://ci.nodejs.org/job/node-test-pull-request/59377/
Landed in 2079a7aec4323e32ed4ae72bc56ac9125c72fe79
Backport in https://github.com/nodejs/node/pull/56927