问题描述
我用Angular创建了一个新的Electron-Project.我使用Angular CLI构建应用程序.现在,我想从Renderer-Process到Main-Process进行通信,并在Dev-Tools中遇到错误:
I've created a new Electron-Project with Angular. I build my app with the Angular CLI.Now, I want to communicate from Renderer-Process to Main-Process and get an error in Dev-Tools:
> Uncaught TypeError: fs.existsSync is not a function
at Object.<anonymous> (vendor.bundle.js:72643)
at Object.splitPathRe (vendor.bundle.js:72649)
at __webpack_require__ (inline.bundle.js:53)
at Object.399 (main.bundle.js:54)
at __webpack_require__ (inline.bundle.js:53)
at Object.400 (main.bundle.js:107)
at __webpack_require__ (inline.bundle.js:53)
at Object.291 (main.bundle.js:24)
at __webpack_require__ (inline.bundle.js:53)
at Object.473 (main.bundle.js:234)
at __webpack_require__ (inline.bundle.js:53)
at webpackJsonpCallback (inline.bundle.js:24)
at main.bundle.js:1
我使用此项目模板: https://github.com/auth0-blog/角2-电子重现此错误的步骤是:
I use this Project-Template: https://github.com/auth0-blog/angular2-electronThe steps to reproduce this error are:
git clone https://github.com/auth0-blog/angular2-electron
npm install
3.将以下行添加到src/app/app.component.ts
3.Add following line to src/app/app.component.ts
const {ipcRenderer} = require('electron');
没有该行,该应用程序运行就没有任何问题.由于电子,我必须以这种方式引用ipcRenderer ... https://github.com/electron/electron/blob/master/docs/api/ipc-main.md
Without that line, the app runs without any problems.Due to electron I have to reference the ipcRenderer that way... https://github.com/electron/electron/blob/master/docs/api/ipc-main.md
我不知道我在做什么错,希望您能帮助我.
I have no idea what I am doing wrong and hope, you can help me.
推荐答案
Webpack带来了自己的require
,它掩盖了node.js'require
,因此,当您require
一个webpack可以使用的node.js核心模块时,解析为您的文件或依赖项之一. (您可以在堆栈跟踪中看到它包含__webpack_require__
.这是因为webpack将所有require
重写为__webpack_require__
,以便它使用其自己的内部node.js-esque系统). Webpack是为Web/浏览器构建的,因此在开箱即用的情况下无法很好地运行.要解决它,您可以使用以下代码: https://github.com/chentsulin/webpack -target-electron-renderer .
Webpack brings its own require
which clobbers node.js' require
, so when you require
a node.js core module that webpack can't resolve to one of your files or dependencies, it throws. (You can see in your stack trace that it includes __webpack_require__
. That's because webpack rewrites all require
s to __webpack_require__
so that it uses it's own internal node.js-esque system). Webpack is built for the web/browsers so it doesn't play well with node out of the box. To get around it you can use this: https://github.com/chentsulin/webpack-target-electron-renderer.
但是我也将考虑完全使用webpack,请参阅:为什么将webpack与电子一起使用
But I'd also consider using webpack at all, see: why use webpack with electron
这篇关于电子与角度:fs.existsSync不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!