electron: Can't run command line tool using child_process after packaged

  • Electron version: 1.4.4
  • Operating system: OSX 10.11.6

The user scenario is like this:

  1. user selects a directory
  2. use node’s child_process to run npm install under this directory

The code snippets related to running command line is: (ES6)

import cp from 'child_process';
import { ipcRenderer } from 'electron';

export function runCommand(command, option, done) {
  const child = cp.exec(command, option);
  ipcRenderer.sendSync('newChildProcess', child.pid);
  done({
    type: 'id',
    data: child.pid
  });
  child.stdout.on('data', (data) => {
    done({
      type: 'stdout',
      data
    });
  });
  child.stderr.on('data', (data) => {
    done({
      type: 'stdout',
      data
    });
  });
  child.on('close', (code) => {
    done({
      type: 'close',
      data: code
    });
  });
}

In development phase, it works well. But after I package the code as dmg on Mac, the exec throw a “/bin/sh: npm command not found” error with an exit code 127. I’m sure I have installed npm on my machine.

Is there anyone know how to fix this?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 6
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

So here comes the question: where does this process.env.PATH come from inside the packaged app? Is there a method to change it?

You might want to take a look at using something like https://github.com/sindresorhus/fix-path

Closing this out.

I found a note on official document, is that the reason? image

Does that means if I packaged the app as asar, I can’t use child_process.exec to execute command line tool in my app?

So here comes the question: where does this process.env.PATH come from inside the packaged app? Is there a method to change it?

You might want to take a look at using something like https://github.com/sindresorhus/fix-path

Closing this out.

npm install -save fix-path It works fine when I run electron . from my-app. However, once packaged I see “Error: Cannot find module” how do it ??