Skip to content

cjs context globals are available unexpected in ESM when using node --eval 'import("pkg")' #57885

Description

@JounQin

Version

v22.13.1

Platform

Linux mightyiam-eslintconfigl-deiiy42chvs 6.1.91-060191-generic #202407021948 SMP PREEMPT_DYNAMIC Tue Jul  2 20:28:47 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

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?

node:internal/modules/cjs/loader:1244
  const err = new Error(message);
              ^

Error: Cannot find module '../package.json'
Require stack:
- /tmp/L5XoA9/[eval]
    at Function._resolveFilename (node:internal/modules/cjs/loader:1244:15)
    at Function._load (node:internal/modules/cjs/loader:1070:27)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Module.require (node:internal/modules/cjs/loader:1335:12)
    at require (node:internal/modules/helpers:136:16)
    at file:///tmp/L5XoA9/node_modules/eslint-plugin-import-x/lib/meta.js:5:34
    at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:547:26)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:436:15) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/tmp/L5XoA9/[eval]' ]
}

Node.js v22.13.1

Additional information

eslint-plugin-import-x is a dual package

Activity

  1. JounQin commented on Apr 15, 2025

    @JounQin
    ContributorAuthor

    Running with Worker eval also has this issue, the difference is that __filename been [eval] and [worker eval].

  2. aduh95 commented on Apr 19, 2025

    @aduh95
    Contributor

    Can you send a repro that doesn't involve any npm command / downloading code from the internet?

  3. added
    moduleIssues and PRs related to the module subsystem.
    on Apr 19, 2025
  4. JounQin commented on Apr 28, 2025

    @JounQin
    ContributorAuthor

    @aduh95 Here we go: https://github.com/JounQin/test/tree/repro/node_esm

    node index.js

    node 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]
    
  5. aduh95 commented on Apr 28, 2025

    @aduh95
    Contributor

    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 __filename is 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:

  6. JounQin commented on Apr 28, 2025

    @JounQin
    ContributorAuthor

    It is indeed the case that __filename is global in this context (and always has been AFAIR)

    But it's unavailable with node index.js, what means the behavior is inconsistent.

  7. aduh95 commented on Apr 28, 2025

    @aduh95
    Contributor

    It's the same for all other --eval only globals, such as assert, fs, module, exports, etc.

  8. github-actions commented on Apr 20, 2026

    @github-actions
    Contributor

    This 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.

  9. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Apr 20, 2026
  10. github-actions commented on May 20, 2026

    @github-actions
    Contributor

    This 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    moduleIssues and PRs related to the module subsystem.staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions