How to make electron-prebuilt respect NODE_PATH?


#1

My Electron application has the following directory structure:

app 
  | node_modules
  |  | electron-prebuilt
  | output
  |  | Prelude
  main.js

If I run NODE_PATH=output node I can use require('Prelude').
If main.js requires Prelude though, I get the following Error when running electron:

$ NODE_PATH=output node_modules/.bin/electron main.js
Error opening app
The app provided is not a valid electron app, please read the docs on how to write one:
https://github.com/atom/electron/tree/master/docs

Error: Cannot find module 'Prelude'

If I copy the contents of `output/` into `node_modules/` the app starts fine but I want to keep the two directories seperate. How can I make electron respect NODE_PATH or add folders to it's require path.

Edit

[quote="kRITZCREEK, post:7, topic:21757, full:true"]
For anyone struggling like I did, I finally found a solution here; https://github.com/atom/electron/issues/11

Adding this to the top of my main.js file that starts the Electron application solved it.

//main.js
require('module').globalPaths.push(__dirname + "/output");
var app = require('app');  // Module to control application life.
...
[/quote]

#2

Have you tried running the command specifying the app’s directory instead of main.js?


#3

I did and got the same result :frowning:


#4

It says here that Node will search for modules in the paths in the NODE_PATH environment variable if it is a list of absolute paths. You’re setting it to a relative path, so that may be the reason Node can’t find your module.


#5

Setting it to an absolute path doesn’t help it. Starting the node interpreter with the relative path set actually works. But I made sure to try the absolute path aswell.


#6

Why don’t you require the relative path to the module: require('./out/Prelude')?


#7

For anyone struggling like I did, I finally found a solution here; https://github.com/atom/electron/issues/11

Adding this to the top of my main.js file that starts the Electron application solved it.

//main.js
require('module').globalPaths.push(__dirname + "/output");
var app = require('app');  // Module to control application life.
...

#8

Sorry for the late answer…

The reason I couldn’t do that is because the content of ./output is transpiler output and has multiple modules that link in between one another by name.

Because I don’t want to change all the require's in that transpiler output I had to find a way to make the resolver find things inside ./output.

Thanks a lot for your time and help!