Uncaught TypeError: fs.existsSync is not a function错误

错误代码

Uncaught TypeError: fs.existsSync is not a function

    at getElectronPath (index.js?bdb9:7)

    at eval (index.js?bdb9:18)

    at Object../node_modules/electron/index.js (chunk-vendors.js:3159)

    at __webpack_require__ (app.js:854)

    at fn (app.js:151)

    at eval (reply.js?17a1:1)

    at Module../src/ipc/reply.js (app.js:1145)

    at __webpack_require__ (app.js:854)

    at fn (app.js:151)

    at eval (cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/App.vue?vue&type=script&lang=js&:4)

在 Electron 的Issue #7300中找到了解决方案,作为一名 Electron 以及 Web 前端的初学者,自然要在此问题上稍加分析,争取多了解一些背景知识,提高学习质量。

此问题出现的原因为:nodejs 运行时的 require 与编译时 webpack 的 require 是不同的。默认情况下,window是全局的,然而在 webpack 编译时会忽略window。

其他的解决方案:使用 preload 方法

mainWindow =new BrowserWindow({

    width: 800,

    height: 600,

    webPreferences: {

      nodeIntegration: false,

      preload: __dirname + '/preload.js'    }

  });

在 preload.js 文件中将要使用的模块引入即可

window.ipcRenderer = require('electron').ipcRenderer;

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容