0

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.

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

ReferenceError: require is not defined

I tried some solutions like importing 'require.js' but nothing is working.

<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
Zoe
  • 27,060
  • 21
  • 118
  • 148
TaouBen
  • 1,165
  • 1
  • 15
  • 41
  • 2
    isn't Electron in NodeJS – MatejMecka Mar 06 '18 at 17:23
  • Yes.. But how is that gonna help ? – TaouBen Mar 06 '18 at 20:42
  • Possible duplicate of [Electron require() is not defined](https://stackoverflow.com/questions/44391448/electron-require-is-not-defined) – Puka Aug 25 '19 at 08:55
  • I don't think I've downvoted your question, I've just happened to realize that your question was not answered and that this one: https://stackoverflow.com/questions/44391448/electron-require-is-not-defined was, so I thought I'd mark yours as a duplicate of to help you, and other people, find the right answer. I'm sorry if I offended you @TaouBen , but that was certainly not my intention. – Puka Aug 26 '19 at 08:11

1 Answers1

0

You can wrap the require calls in an if block and check if require is defined:

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.