13

Using electron in combination with Angular2, Typescript and Electron I am try to find out how to use a node module package installed via npm. The current problem is that I have to specify the location of the module like var module = require('./node_modules/xyz/lib/xyz.js'). But then electron does not find the dependencies of xyz, which are located within ./node_modules/xyz/node_modules/yyy and complains ./yyy.js can not be found.

The electron app structure

dist  
├── angular2.dev.js   
├── config.js  
├── index.html  
├── main.js  
├── node_modules  
├── package.json  
└── app.js  
chrisber
  • 730
  • 1
  • 12
  • 27
  • Turns out that I used npm to install modules, but I JSPM with Angular2 to load modules. when installing modules with `jspm install npm:xyz` then I can require modules in electron like require('xyz') . – chrisber Jun 06 '15 at 12:19
  • I think I know what the problem is, a similar question has been asked and I think [this answer](http://stackoverflow.com/a/32335874/2295964) might help you! – Yan Foto Sep 03 '15 at 09:10

4 Answers4

7

UPDATE:

A similar question has been asked and my answer would most probably help you here:

If you don't append the path to your app node_modules directory under your app root to the NODE_PATH variable it is not going to work. So you need to do something like this:

export NODE_PATH=/PATH/TO/APP/node_modules
electron /PATH/TO/APP

When exporting NODE_PATH make sure that you provide an absolute path.


If electron cannot find modules when you require them normally, it's a sign that your package.json doesn't contain the modules as dependency even if the module is already available under your dist directory.

So make sure that you are inside dist directory and use

npm install --save xyz

note the --save flag!

Community
  • 1
  • 1
Yan Foto
  • 10,850
  • 6
  • 57
  • 88
1

The current problem is that I have to specify the location of the module like var module = require('./node_modules/xyz/lib/xyz.js')

You should be able to do var module = require('xyz'); If you have it locates in the relative path ./node_modules/ .... that you mentioned.

basarat
  • 261,912
  • 58
  • 460
  • 511
1

If you didn't include the modules in your package.json I found it easiest to just copy all of them to node_modules in your release. It's something like releases > ARCHITECTURE > resources > node_modules

Harry Merzin
  • 406
  • 1
  • 4
  • 10
1

According to electron's docs, set the nodeIntegration preference to true. Do this by adding

webPreferences: {
  nodeIntegration: true
}

to your createWindow() function.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    This is not what you think. Node integration means you expose Node.js in the frontend, making your app especially vulnerable, security wise. –  Jan 28 '22 at 22:32