fs module fails when integrating Electron into Angular project

扶醉桌前 提交于 2019-12-04 07:40:10

As @ccnokes pointed it out in this answer:

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 requires 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.

I would suggest using ngx-electron that appear to be somewhat not maintained (last commit was a year ago) but still works as a charm and will let you use a lot of Electron abilities in Angular (like in this answer's comment).

You could also try this workaround by Vjekoslav Ratkajec:

In your index.html

<script>  const electron = require('electron');  </script>

Then in any of your Typescript files:

declare const electron: any;

The downside is that you won't enjoy Typescript support.

You also could use webpack-target-electron-renderer to tell wich require to import with electron or webpack but I haven't tried it or this boilerplate if you want to start your project from scratch !

Hope it'll help

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