node icon indicating copy to clipboard operation
node copied to clipboard

module: do not set CJS variables for Worker eval

Open aduh95 opened this issue 1 year ago β€’ 6 comments

There are no local CJS variables in the Worker eval context – it's all global variables, just like in the --eval context.

aduh95 avatar May 18 '24 21:05 aduh95

Review requested:

  • [ ] @nodejs/loaders

nodejs-github-bot avatar May 18 '24 21:05 nodejs-github-bot

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.

GeoffreyBooth avatar May 19 '24 03:05 GeoffreyBooth

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.

dygabo avatar May 19 '24 03:05 dygabo

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.

aduh95 avatar May 20 '24 11:05 aduh95

CI: https://ci.nodejs.org/job/node-test-pull-request/59373/

nodejs-github-bot avatar May 23 '24 14:05 nodejs-github-bot

CI: https://ci.nodejs.org/job/node-test-pull-request/59377/

nodejs-github-bot avatar May 23 '24 16:05 nodejs-github-bot

Landed in 2079a7aec4323e32ed4ae72bc56ac9125c72fe79

nodejs-github-bot avatar May 25 '24 02:05 nodejs-github-bot

Backport in https://github.com/nodejs/node/pull/56927

joyeecheung avatar Feb 06 '25 00:02 joyeecheung