Skip to content

"ReferenceError: process is not defined" when using ESM mode and contextIsolation is not enabled #46142

Closed

Description

@haohetao

Preflight Checklist

Electron Version

34.0.0-35.0.2

What operating system(s) are you using?

Windows

Operating System Version

Windows11

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

process object is exists

Actual Behavior

"ReferenceError: process is not defined"

Image

Testcase Gist URL

https://gist.github.com/nikwen/2e17b8860610c514210fa8ee133d2f33

Additional Information

preload.mjs

......
console.log(navigator.language)
console.log(process)
......

main.mjs

const createWindow = () => {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      sandbox: false,
      contextIsolation: false,
      preload: join(__dirname, 'preload.mjs')
    }
  })

  win.loadFile('index.html')
  win.on('ready-to-show', () => {
    win.webContents.openDevTools()
  })
}

This issue may be related to #46123

Some global objects are missing when ESM is on and contextIsolation is false

Activity

  1. added theissue type on Mar 20, 2025
  2. nikwen commented on Mar 20, 2025

    @nikwen
    Member

    Does nodeIntegration: true solve the issue?

  3. haohetao commented on Mar 20, 2025

    @haohetao
    Author

    Does nodeIntegration: true solve the issue?

    Yes,when I add nodeIntegration: true the problem disappears

  4. nikwen commented on Mar 20, 2025

    @nikwen
    Member

    The renderer and the preload script only have access to Node.js modules (such as process) with nodeIntegration: true.

    That said, for security reasons, it is recommended to

    • Set sandbox: true, contextIsolation: true, nodeIntegration: false
    • Use Node.js modules only in the main thread
    • Communicate between renderer and main thread via the context bridge and IPC

    See:

    Since this is working as expected, I'll close this issue.

  5. haohetao commented on Mar 20, 2025

    @haohetao
    Author

    @nikwen

    The renderer and the preload script only have access to contextIsolation (such as process) with nodeIntegration: true.

    However, when commonjs mode, the process can be accessed even without nodeIntegration: true,This is an ESM-related issue. In theory, nodeIntegration: true should only affect scripts in the web page.

  6. nikwen commented on Mar 20, 2025

    @nikwen
    Member

    Reopening in case I misunderstood something myself.

  7. reopened this on Mar 20, 2025
  8. nikwen commented on Mar 20, 2025

    @nikwen
    Member

    Sorry for the confusion during the night! Thanks for correcting me.

    I can confirm that process is defined in the preload script with CommonJS and contextIsolation disabled (testcase gist) and that it is not defined with ESM and contextIsolation disabled (testcase gist).

    Again, sorry for the confusion! I spoke too fast there.

    I'll leave this for another team member to review who is more familiar with how sandbox, contextIsolation and nodeIntegration interact.

  9. added
    has-repro-gist
    and removed on Mar 20, 2025
  10. clavin commented on Apr 3, 2025

    @clavin
    Member

    This indeed appears to be a bug. It's a shot in the dark, but I suspect we might be deleting these globals too early.

    I also want to emphasize that we recommend using the sandbox and context isolation features of Electron, as @nikwen said above.

  11. added
    status/confirmed
    on Apr 3, 2025
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

    34-x-yESMbug 🪲has-repro-gistplatform/allstatus/confirmed

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions