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函数出错。 检查代码,有没有正确引入
不知道你这个问题是否已经解决, 如果还没有解决的话:所有的帖子都是一样的,都不去试一下 ,这偏文章我把坑都走了一遍。
直接上代码
最主要的就是这个问题 const electron = require(‘electron’).ipcRenderer 能不用引用
这个问题解决了 后面的就简单了
这么多帖子 居然没有一个说这个问题
代码在gitee上 地址在最下面
直接引用就会报错
#Uncaught TypeError: fs.existsSync is not a function
根据参考资料和问题描述,我可以给出以下解决方案:
根据参考资料段落2中的提示,问题可能是因为在渲染进程中没有集成node环境导致无法使用fs包和require关键字。解决这个问题的方法是在主进程中开启远程对象,并在渲染进程中引入remote。
main.js
文件中,找到创建窗口的代码段,并增加enableRemoteModule: true
选项。let win = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true // 添加这一行
}
})
window.require
引入electron的remote模块。const { remote } = window.require('electron')
通过以上两步的操作,应该可以解决页面空白和报错的问题。请注意,不要使用require('electron')
语法引入remote模块,而是使用window.require
。