Electron旧项目启动报错:require未定义及依赖兼容问题求助
旧Electron项目运行问题排查与解决
问题背景
尝试运行旧Electron项目,无法确定适配的Node版本,也不清楚依赖更新优先级。已添加如下webPreferences配置但问题未解决:
webPreferences: { nodeIntegration: true, nodeIntegrationInWorker: true, contextIsolation: false, enableRemoteModule: true, },
yarn dev运行日志
yarn run v1.22.19 $ webpack-dev-server --hot --watch --config webpack.config.dev.js i 「wds」: Project is running at http://localhost:8080/ i 「wds」: webpack output is served from http://localhost:8080/ i 「atl」: Using typescript@3.4.4 from typescript i 「atl」: Using tsconfig.json from ./tsconfig.json i 「atl」: Using typescript@3.4.4 from typescript i 「atl」: Using tsconfig.json from ./tsconfig.json i 「atl」: Checking started in a separate process... i 「atl」: Time: 3514ms i 「atl」: Checking started in a separate process... i 「atl」: Time: 2355ms ‼ 「wdm」: Hash: 4697290ef706a2a99ad5e1b5fab076995682e95f Version: webpack 4.30.0 Child Hash: 4697290ef706a2a99ad5 Time: 17242ms Built at: 2023-05-24 15:55:15 Asset Size Chunks Chunk Names bundle.js 6.53 MiB main [emitted] main Entrypoint main = bundle.js [0] multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/index.tsx 52 bytes {main} [built] [./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built] [./node_modules/mousetrap/mousetrap.js] 33.1 KiB {main} [built] [./node_modules/react-dom/index.js] 1.33 KiB {main} [built] [./node_modules/react-redux/es/index.js] 416 bytes {main} [built] [./node_modules/react/index.js] 190 bytes {main} [built] [./node_modules/strip-ansi/index.js] 161 bytes {main} [built] [./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 8.26 KiB {main} [built] [./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.59 KiB {main} [built] [./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.05 KiB {main} [built] [./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built] [./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.61 KiB {main} [built] [./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 75 bytes {main} [built] [./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built] [./src/index.tsx] 7.99 KiB {main} [built] + 695 hidden modules WARNING in ./node_modules/fluent-ffmpeg/lib/options/misc.js 27:21-40 Critical dependency: the request of a dependency is an expression @ ./node_modules/fluent-ffmpeg/lib/fluent-ffmpeg.js @ ./node_modules/fluent-ffmpeg/index.js @ ./src/common/Util.js @ ./src/views/Duplicates.tsx @ ./src/index.tsx Child Hash: e1b5fab076995682e95f Time: 13857ms Built at: 2023-05-24 15:55:11 Asset Size Chunks Chunk Names background.js 4.33 MiB main [emitted] main Entrypoint main = background.js [0] multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./background.ts 52 bytes {main} [built] [./background.ts] 1010 bytes {main} [built] [./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built] [./node_modules/strip-ansi/index.js] 161 bytes {main} [built] [./node_modules/webpack-dev-server/client/index.js?http://localhost:8080] (webpack)-dev-server/client?http://localhost:8080 8.26 KiB {main} [built] [./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.59 KiB {main} [built] [./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.05 KiB {main} [built] [./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built] [./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.61 KiB {main} [built] [./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 75 bytes {main} [built] [./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built] [./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.11 KiB {main} [built] [./src/background/BackgroundWindow.ts] 10.1 KiB {main} [built] [./src/main/Logger.js] 1010 bytes {main} [built] [querystring] external "querystring" 42 bytes {main} [built] + 586 hidden modules WARNING in ./node_modules/fluent-ffmpeg/lib/options/misc.js 27:21-40 Critical dependency: the request of a dependency is an expression @ ./node_modules/fluent-ffmpeg/lib/fluent-ffmpeg.js @ ./node_modules/fluent-ffmpeg/index.js @ ./src/background/ScreenshotEngine.ts @ ./src/background/BackgroundWindow.ts @ ./background.ts WARNING in ./node_modules/chokidar/lib/fsevents-handler.js Module not found: Error: Can't resolve 'fsevents' in '.\node_modules\chokidar\lib' @ ./node_modules/chokidar/lib/fsevents-handler.js @ ./node_modules/chokidar/index.js @ ./src/background/Watcher.ts @ ./src/background/BackgroundWindow.ts @ ./background.ts i 「wdm」: Compiled with warnings.
yarn dev控制台报错
Uncaught ReferenceError: require is not defined at eval (external_"querystring":1:18) at Object.querystring (bundle.js:8870:1) at __webpack_require__ (bundle.js:703:30) at fn (bundle.js:77:20) at Object.eval (webpack:///(:8080/webpack)-dev-server/client?:6:19) at eval (webpack:///(:8080/webpack)-dev-server/client?:299:30) at ./node_modules/webpack-dev-server/client/index.js?http://localhost:8080 (bundle.js:7798:1) at __webpack_require__ (bundle.js:703:30) at fn (bundle.js:77:20) at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:1:1)
yarn start运行日志
yarn run v1.22.19 $ cross-env NODE_ENV=development electron . process.env.NODE_ENV: development Done in 72.06s.
yarn start控制台报错
bundle.js:1 Failed to load resource: net::ERR_CONNECTION_REFUSED .\node_modules\electron\dist\resources\electron.asar\renderer\security-warnings.js:170 Electron Security Warning (Insecure Content-Security-Policy) This renderer process has either no Content Security Policy set or a policy with "unsafe-eval" enabled. This exposes users of this app to unnecessary security risks. For more information and help, consult https://electronjs.org/docs/tutorial/security. This warning will not show up once the app is packaged.
当前Node版本
$ nvm list 16.10.0 14.21.3 14.17.0 12.4.0 * 10.15.3 (Currently using 64-bit executable) 10.8.0 10.3.0 9.11.2 8.9.4
后续疑问与尝试
- 使用Node 10.15.3适配依赖,但仍不确定版本选择与依赖更新优先级:优先更新Electron、webpack&TypeScript,还是先处理fluent-ffmpeg这类带警告的依赖?有没有JS依赖兼容的集中参考或冲突排查工具?
- 使用Electron 5+Node 12.18时,renderer.js无法获取node版本,报
Uncaught ReferenceError: versions is not defined - 更新Electron到v6后,报
Error: contextBridge API can only be used when contextIsolation is enabled;开启contextIsolation后能获取版本,但加载应用bundle仍报错:是否需要通过preload.js的contextBridge暴露所有IpcHandlers与src/common下的文件?还是webpack配置/路径问题?添加ping handler时又报ipcMain.handle is not a function,是否需要把所有require替换为require('electron').remote.require?
解决方案
1. 锁定适配的Node版本
Electron与Node版本是绑定的,先查项目原package.json中的Electron版本,对应官方绑定的Node版本:
- Electron 5对应Node 12.0.0,Electron 6对应Node 12.4.0
- 先恢复项目原Electron版本,使用对应绑定的Node版本运行,避免基础环境不兼容
2. 优先解决核心运行错误
require is not defined报错:在webpack配置中添加target: 'electron-renderer',让webpack识别Electron环境,正确处理Node内置模块(如querystring)bundle.js加载失败:确保yarn dev先启动dev server,Electron窗口的loadURL设置为http://localhost:8080,与dev server地址一致
3. 依赖更新优先级
- 先更新Electron、webpack、TypeScript:这三个是项目核心构建与运行环境,版本不匹配会引发大部分兼容性问题。更新时遵循Electron与webpack的兼容表,TypeScript版本要与webpack-loader适配
- 再处理fluent-ffmpeg、chokidar这类依赖警告:
fsevents是macOS专属模块,Windows下可忽略;fluent-ffmpeg的动态依赖警告可通过webpack的IgnorePlugin或调整依赖版本解决
4. Electron版本升级后的适配
ipcMain.handle是Electron 7+新增API,升级到v6时会报错,改用ipcMain.on结合ipcRenderer.sendSync,或直接升级到Electron 7+- 开启
contextIsolation后,必须通过preload.js暴露需要的API,不能直接在renderer中使用require或remote模块:
同时更新webPreferences配置:// preload.js const { contextBridge, ipcRenderer } = require('electron'); contextBridge.exposeInMainWorld('electronAPI', { ping: () => ipcRenderer.invoke('ping'), // 其他需要暴露的API });webPreferences: { contextIsolation: true, preload: path.join(__dirname, 'preload.js') }
内容的提问来源于stack exchange,提问作者belgacea
相关产品推荐
相关产品推荐

