简体   繁体   English

要求未在Electron中定义?

[英]require is not defined in Electron?

I am trying to build a web-desktop application using ElectronJs and AngularJS, so I wrote my code and everything worked fine when I start my desktop application, but in the browser, I have a problem - I have an undefined method. 我试图使用ElectronJs和AngularJS构建Web桌面应用程序,因此我编写了代码,启动桌面应用程序时一切正常,但是在浏览器中,我遇到了问题-我有一个未定义的方法。

Here is the line where I am having a problem : 这是我遇到问题的那一行:

const electron = require('electron')

Everything works fine as I said when I tap my command : 就像我点击命令时所说的那样,一切正常:

electron .

But when I open my file index.html in my browser I get this error in my console 但是,当我在浏览器中打开文件index.html时,在控制台中出现此错误

ReferenceError: require is not defined

I tried some solutions like importing 'require.js' but nothing is working. 我尝试了一些解决方案,例如导入“ require.js”,但没有任何效果。

<script src="require.js"></script>

But I get another error which is : 但我得到另一个错误是:

Error: Module name "electron" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded

You can wrap the require calls in an if block and check if require is defined: 您可以将require调用包装在if块中,并检查是否定义了require:

if (typeof require === 'function') {
    const electron = require('electron')
}

You don't want to be requiring electron (or any other Node modules) if you're running in a browser anyway. 无论如何,如果您正在浏览器中运行,则不需要电子(或其他任何Node模块)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM