Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm: command not found when executing commands in Electron app

I'm working on an electron app and within the app, I execute shell commands using child_process.exec. One of the commands I run is npm run start; this works perfectly in a dev environment but when I build the application for production all npm commands fail with showing the following error:

   Error: Command failed: npm run start
   /bin/sh: npm: command not found


    at ChildProcess.exithandler (child_process.js:287)
    at emitTwo (events.js:126)
    at ChildProcess.emit (events.js:214)
    at maybeClose (internal/child_process.js:925)
    at Socket.stream.socket.on (internal/child_process.js:346)
    at emitOne (events.js:116)
    at Socket.emit (events.js:211)
    at Pipe._handle.close [as _onclose] (net.js:554)

I tried running the application in debug mode by running the following command open MyApp.app/Contents/MacOS/MyApp and the npm commands run successfully with no errors.

What could be the issue?

like image 911
HackAfro Avatar asked Mar 29 '19 09:03

HackAfro


1 Answers

The issue that the environment variable of $PATH is wrong inside the packaged app, it works in development because the application is launched from the terminal which gives it access to the $BASH profile.

To solve this problem I used this package fix-path. I installed the package and added the following snippet at the top of the file

if (process.env.NODE_ENV === 'production') {
  const fixPath = require('fix-path');

  fixPath();
}

I came to this answer after going through this issue on GitHub. Thanks to @Seblor

like image 120
HackAfro Avatar answered Oct 22 '22 20:10

HackAfro