forge icon indicating copy to clipboard operation
forge copied to clipboard

No good way to have one renderer process with Node integration and one renderer process without Node integration

Open george-thomas-hill opened this issue 3 years ago • 2 comments

Pre-flight checklist

  • [X] I have read the contribution documentation for this project.
  • [X] I agree to follow the code of conduct that this project uses.
  • [X] I have searched the issue tracker for a bug that matches the one I want to file, without success.

Electron Forge version

6.0.0-beta.61

Electron version

v15.2.0

Operating system

Kubuntu 21.04 x64

Last known working Electron Forge version

No response

Expected behavior

I am trying to configure electron-forge to allow me to have one visible renderer window as my UI (with Node integration turned off) and one hidden renderer window as a worker window (with Node integration turned on).

I am doing this because I want to use the Chokidar npm module in my hidden worker renderer window.

I can get this to work in production mode (i.e. with npm run make) but not development mode if I do two things:

  • I use const chokidar = require("chokidar") in my worker process.
  • I set target: 'electron-renderer' in webpack.renderer.config.

Alternatively, I can get this to work in development mode (i.e. with npm start) but not in production mode if I make the following changes:

  • I use const chokidar = window.require("chokidar") in my worker process.
  • I remove target: 'electron-renderer' from webpack.renderer.config.

To my knowledge, there is no configuration that works for both production mode and development mode.

Actual behavior

If I configure this to work in production mode, then in development mode I get the following error in my UI window:

Uncaught ReferenceError: require is not defined
    at Object.url (index.js:486)
    at __webpack_require__ (index.js:515)
    at fn (index.js:702)
    at eval (parseURL.js:5)
    at Module../node_modules/webpack-dev-server/client/utils/parseURL.js (index.js:292)
    at __webpack_require__ (index.js:515)
    at fn (index.js:702)
    at eval (index.js?protocol=ws%3A&hostname=0.0.0.0&port=3000&pathname=%2Fws&logging=info:7)
    at Module../node_modules/webpack-dev-server/client/index.js?protocol=ws%3A&hostname=0.0.0.0&port=3000&pathname=%2Fws&logging=info (index.js:206)
    at __webpack_require__ (index.js:515)

If I configure this to work in development mode, then in production mode I get the following error in my worker window:

Uncaught Error: Cannot find module 'chokidar'
Require stack:
- /usr/lib/my-new-app-2/resources/app/.webpack/renderer/worker_window/index.html
    at Module._resolveFilename (node:internal/modules/cjs/loader:934)
    at Function.o._resolveFilename (node:electron/js2c/renderer_init:29)
    at Module._load (node:internal/modules/cjs/loader:779)
    at Function.c._load (node:electron/js2c/asar_bundle:5)
    at Function.o._load (node:electron/js2c/renderer_init:29)
    at Module.require (node:internal/modules/cjs/loader:1006)
    at require (node:internal/modules/cjs/helpers:93)
    at worker.js:2

Steps to reproduce

Minimal code configured to work in production mode is available at:

https://github.com/george-thomas-hill/my-new-app-2-works-in-production

Minimal code configured to work in development mode is available at:

https://github.com/george-thomas-hill/my-new-app-2-works-in-development

Additional information

This may be related to the Enhancement request posted at:

https://github.com/electron-userland/electron-forge/issues/2570

However, I believe that this qualifies as a bug because it is an instance of electron-forge not working for a common use case.

george-thomas-hill avatar Oct 24 '21 11:10 george-thomas-hill

Hello @george-thomas-hill, did you find an other solution to make this works? I could not make this works with your workaround..

Quelu avatar Dec 31 '21 14:12 Quelu

Hello, @Quelu.

I ended up using Node's child_process.fork() to make a separate Node-powered worker process.

https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options

I'm sorry for the slow response!

george-thomas-hill avatar Jan 09 '22 12:01 george-thomas-hill

closed in https://github.com/electron/forge/pull/2867

erickzhao avatar Nov 01 '22 00:11 erickzhao