Repository navigation
cjs context globals are available unexpected in ESM when using node --eval 'import("pkg")' #57885
Description
Activity
Running with
Workerevalalso has this issue, the difference is that__filenamebeen[eval]and[worker eval].Can you send a repro that doesn't involve any npm command / downloading code from the internet?
- addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Apr 19, 2025 @aduh95 Here we go: https://github.com/JounQin/test/tree/repro/node_esm
node index.jsnode index.js file:///Users/JounQin/Workspaces/GitHub/test/index.js:1 console.log(__filename) ^ ReferenceError: __filename is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and '/Users/JounQin/Workspaces/GitHub/test/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. at file:///Users/JounQin/Workspaces/GitHub/test/index.js:1:13 at ModuleJob.run (node:internal/modules/esm/module_job:263:25) at async ModuleLoader.import (node:internal/modules/esm/loader:540:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:117:5)node -e 'import("./index.js")[eval]Instead of a full fledge repo, I find it easier to send a script that create the required parts instead of letting the maintainers figure out for themselves what are the important parts.
mkdir repro cd repro echo 'console.log(__filename)' > index.js echo '{ "type": "module" }' > package.json ! node index.js node -p 'import("./index.js")' cd .. rm -r repro
It is indeed the case that
__filenameis global in this context (and always has been AFAIR), and the documentation is not clear on that point. If we were to change the behavior, I think we have three options, ordered from most breaking to least breaking:- not expose any
__filename - use a similar trick as we're doing for
cryptosince lib: enable global WebCrypto by default #42083 - keep the current behavior, only update the docs
Reacted by JounQin- not expose any
It is indeed the case that
__filenameis global in this context (and always has been AFAIR)But it's unavailable with
node index.js, what means the behavior is inconsistent.It's the same for all other
--evalonly globals, such asassert,fs,module,exports, etc.Reacted by JounQingithub-actions commented
on Apr 20, 2026 on Apr 20, 2026 – with GitHub ActionsContributorMore actionsThis issue has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Apr 20, 2026 github-actions commented
on May 20, 2026 on May 20, 2026 – with GitHub ActionsContributorMore actionsThis issue has been automatically closed after 30 days of inactivity following its stale status (no activity for a total of 240 days).
If this is still relevant, feel free to reopen it or leave a comment with additional details so we can continue the discussion.
Version
v22.13.1
Platform
Subsystem
No response
What steps will reproduce the bug?
un-ts/eslint-plugin-import-x#296
npm install -D eslint-plugin-import-x@4.10.3 node --eval 'import("eslint-plugin-import-x")'How often does it reproduce? Is there a required condition?
consistently
What is the expected behavior? Why is that the expected behavior?
No error
What do you see instead?
Additional information
eslint-plugin-import-xis a dual package