fs.existsSync is not a function when used in electron

烈酒焚心 提交于 2021-02-08 15:12:46

问题


I am using Angular 10, Electron 10.0 and electron-builder v22.8.0.

When starting my Electron app, I receive the following error in the console:

fs.existsSync is not a function when used in electron
    getElectronPath @ ./node_modules/events/events.js:6
    <anonymous> @ ./node_modules/events/events.js:17
    ./node_modules/electron/index.js @ ./node_modules/events/events.js:19
    __webpack_require__ @ ./webpack/bootstrap:79
    ./src/app/projectview/new/new.component.ts @ ./src/app/projectview/new/new.component.ts:1
    [...]
    at __webpack_require__ (bootstrap: 79)

The error pops up here:

It happens when I import electron and have the following line in my renderer process:

import { remote } from 'electron';

// later on in my component:
remote.dialog.showOpenDialog(...);

nodeIntegration is true when creating the BrowserWindow.

   [...]
   win = new BrowserWindow({
      webPreferences: {
          webSecurity: false,
          nodeIntegrationInWorker: true,
          nodeIntegration: true,
          allowRunningInsecureContent: (serve) ? true : false,
    },

I have browsed entire StackOverflow, but can't find any solution I haven't tried. Can anyone help me?


回答1:


so based on your sentence: It happens when I import electron and have the following line in my renderer process: import { remote } from 'electron';

in electron 10 is a breaking change of the remote api.
the webpreference "enableRemoteModule" is now by default false.

activate the module and test again:

const w = new BrowserWindow({
    webPreferences: {
        enableRemoteModule: true
    }
})

here you find all breaking changes

checkout the recommended way to use ipcRenderer:
use ipcRenderer and not remote



来源:https://stackoverflow.com/questions/63269563/fs-existssync-is-not-a-function-when-used-in-electron

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!