Setting NODE_PATH in electron app packaged with electron-forge


#1

In my Electron application, I need to add a user-provided directory to the Node.js package search path.

In a regular Electron app, not set up using electron-forge, I did the following

// See https://discuss.atom.io/t/how-to-make-electron-prebuilt-respect-node-path/21757/8
const configjs = process.argv[2];
const configdirname = path.join(path.dirname(configjs), 'node_modules');
require('module').globalPaths.push(configdirname);

As you see, I used a hint found elsewhere on this forum.

What’s going on is that in AkashaCMS (the static website generator I’ve developed) each project has a configuration object defined in config.js and a set of Node.js packages installed in the node_modules directory. I need require in the Electron app to pull packages out of that directory. I need the user to tell me which project to edit, and hence need a runtime configuration of the Node.js package search path.

As I said, the above idiom works in an Electron app. This evening I’m going through the process of using electron-forge, and now the above idiom is failing.

I have this code currently:

// See https://discuss.atom.io/t/how-to-make-electron-prebuilt-respect-node-path/21757/8
const configjs = "/Users/david/ws/greentransportation.info/config.js";
const configdirname = path.join(path.dirname(configjs), 'node_modules');
require('module').globalPaths.push(configdirname);

console.log(util.inspect(process.argv));
const config = require(configjs);

In this case configjs has a hard-coded path because it seems process.argv is not available when using electron-forge.

Upon doing npm start an error dialog pops up including a stack trace. On the console window these messages are printed:

[ '/Users/david/akasharender/ae/node_modules/electron/dist/Electron.app/Contents/MacOS/Electron',
  '.' ]
App threw an error during load
Error: Couldn't find preset "env" relative to directory "/Users/david/ws/greentransportation.info"
    at /Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
    at Array.map (native)
    at OptionManager.resolvePresets (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/david/akasharender/ae/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at BabelCompiler.compileSync (/Users/david/akasharender/ae/node_modules/electron-compilers/lib/js/babel.js:81:26)

You’ll notice the dump of the process.argv array does not include the path to my project directory – which is computed from the pathname in config.js.

The stack trace shown here matches what’s in the error dialog.


#2

Never mind – it’s not an issue with adding to NODE_PATH … that section of code works. The failure to find the “env” preset is the problem.