require() not defined in Electron HTML page

谁说我不能喝 提交于 2019-12-20 03:36:17

问题


I'm currently unable to load Electron modules in Javascript files outside of the main.js file.

I'm trying to use:

const electron = require('electron');

const ipcRenderer = electron.ipcRenderer;

in a file called settings.js.

I have the correct NPM modules installed (eletron-prebuilt).

The settings page is loaded in a webview like so:

<webview id="settings" src="./settings.html"></webview>

and the settings.js included like:

<script type='text/javascript' src='./resources/js/settings.js'></script>

Running out of ideas here, but I wonder if it's some kind of scoping issue?

Many thanks.


回答1:


You're loading it in a webview, which doesn't support nodeintegration by default.

Add the nodeintegration attribute to your webivew.

<webview id="settings" src="./settings.html" nodeintegration></webview>


来源:https://stackoverflow.com/questions/36859984/require-not-defined-in-electron-html-page

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