6

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?

HackAfro
  • 720
  • 1
  • 12
  • 28

1 Answers1

10

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

HackAfro
  • 720
  • 1
  • 12
  • 28
  • If you find that you don't have a process.env.NODE_ENV, this may help: https://github.com/electron-userland/electron-forge/issues/442#issuecomment-368736955 – cmcculloh Apr 15 '20 at 20:13
  • fix-path didn't solve my issue any help please https://stackoverflow.com/questions/67276969/npx-command-not-found-in-packaged-macos-electron-app – Jithin Zachariah Apr 27 '21 at 06:17
  • 1
    if you don't use ESM, please use version 3 instead – lastStranger Mar 17 '22 at 11:49