electron+vue的坑

vue+electron项目,npm run serve之后打开页面是空白的,控制台报错
Uncaught TypeError: fs.existsSync is not a function
at getElectronPath (index.js?bdb9:7:1)
at Object.eval (index.js?bdb9:18:1)
at eval (index.js:20:30)
at Object../node_modules/electron/index.js (chunk-vendors.js:7768:1)
at webpack_require (app.js:785:30)
at fn (app.js:151:20)
at eval (Home.vue?76f2:101:1)
at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/Home.vue?vue&type=script&lang=js& (app.js:886:1)
at webpack_require (app.js:785:30)
at fn (app.js:151:20)

主进程有问题 , 检查你的主进程吧

这个错误提示表明函数fs.existsSync未被定义,导致了index.js文件中的getElectronPath函数出错。 检查代码,有没有正确引入

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: Vue electron自动更新/ electron手动更新中的 Uncaught TypeError: fs.existsSync is not a function 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    所有的帖子都是一样的,都不去试一下 ,这偏文章我把坑都走了一遍。
    直接上代码

    最主要的就是这个问题 const electron = require(‘electron’).ipcRenderer 能不用引用
    这个问题解决了 后面的就简单了
    这么多帖子 居然没有一个说这个问题
    代码在gitee上 地址在最下面
    在这里插入图片描述

    直接引用就会报错
    #Uncaught TypeError: fs.existsSync is not a function
    在这里插入图片描述

  • 以下回答来自chatgpt:

    根据参考资料和问题描述,我可以给出以下解决方案:

    根据参考资料段落2中的提示,问题可能是因为在渲染进程中没有集成node环境导致无法使用fs包和require关键字。解决这个问题的方法是在主进程中开启远程对象,并在渲染进程中引入remote。

    1. 在主进程的main.js文件中,找到创建窗口的代码段,并增加enableRemoteModule: true选项。
    let win = new BrowserWindow({
      show: false,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false,
        enableRemoteModule: true // 添加这一行
      }
    })
    
    1. 在渲染进程的代码文件中,使用window.require引入electron的remote模块。
    const { remote } = window.require('electron')
    

    通过以上两步的操作,应该可以解决页面空白和报错的问题。请注意,不要使用require('electron')语法引入remote模块,而是使用window.require


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^