npm: command not found when executing commands in Electron app
Asked Answered
M

1

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?

Mnemosyne answered 29/3, 2019 at 9:8 Comment(5)
Is node installed on the non dev machine ?Heartless
I'm running the prod version of the application on my machine which has nide installedMnemosyne
This issue might be related : github.com/electron/electron/issues/7688Heartless
Thanks @Heartless this solved my problem.Mnemosyne
You should create an answer with the complete steps you took to solve your issue, since I only directed you to some github issue. It might be helpful for other people getting the same error.Heartless
M
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

Mnemosyne answered 29/3, 2019 at 9:44 Comment(3)
If you find that you don't have a process.env.NODE_ENV, this may help: github.com/electron-userland/electron-forge/issues/…Invigilate
fix-path didn't solve my issue any help please #67277469Immediate
if you don't use ESM, please use version 3 insteadStrangle

© 2022 - 2024 — McMap. All rights reserved.