Electron-builder: Exec command not found

Created on 5 Nov 2016  Â·  11Comments  Â·  Source: electron-userland/electron-builder

  • Version: 17.21.0

  • Target: dmg

I have some exec() command in my app for example:

exec('npm -v', (err, stdout, stderr) => {

      $log.info(stdout, stderr, err);
    });

It works if i start the app from the shell but it won't if i build the dmg and install the app.

I get error in console: npm command not found

So i tried this:

require('child_process').execSync('which npm').toString();

And i get this error:

child_process.js:538 Uncaught Error: Command failed: which npm


    at checkExecSyncError (child_process.js:495:13)
    at Object.execSync (child_process.js:535:13)
    at Object.childProcess.(anonymous function) [as execSync] (ELECTRON_ASAR.js:690:22)
    at <anonymous>:1:26

Any advice?

Thank you

question

Most helpful comment

Actually i found that using this:

process.env.PATH = process.env.PATH + ':/usr/local/bin';

exec works, could be a fix or it's going to take me to the hell too? 🗡

All 11 comments

An app launched by a GUI launcher (Finder, Dock, Spotlight etc.) receives a pretty empty and useless environment, since standard Unix ways of setting variables via e.g. ~/.profile do not work.

http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x

In any case you cannot assume that npm will be installed on user machine.

@develar Hi, sorry i do not assume that it is installed because it's required that node is already installed on the OS to use the app.

The problem is that I did not understand whether it is possible or not execute this command because the response to the issue that you linked does not provide anything from what I read, am i mistaking?

Also I do not understand what is written on StackOverflow, they are talking about to edit a conf file on the OS, i can't ask final app user to edit the file...

What you suggest sorry?

What you suggest sorry?

"reconstructing the entire shell environment" https://github.com/nodejs/installer/issues/18#issuecomment-230272846

it's not possible due to node installer?

Actually i found that using this:

process.env.PATH = process.env.PATH + ':/usr/local/bin';

exec works, could be a fix or it's going to take me to the hell too? 🗡

could be a fix

Yes. But please note — custom user env will be still not supported. So, "reconstructing the entire shell environment" is the only real complete solution.

@develar sorry not supported where? on electron?

Not supported by your app. For example, if on my machine npm will be installed to another dir. And dir will be specified in the shell PATH.

@develar got it thanks :) do you think it's better to always ask which node or which npm and use the path to the executable anyway to have larger support?

Was this page helpful?
0 / 5 - 0 ratings