shelljs: exec doesn't work in electron

Node version:

6.2.0

ShellJS version

0.7.0

Operating system:

Mac

Description of the bug:

I am trying to run the following command (that I copied from docs) in my electron console.

image

Any idea, why this would be having. As I want to return the version number for checks. Other commands such as which are working but just exec

Thank you! 😃

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 38 (22 by maintainers)

Commits related to this issue

Most upvoted comments

@nfischer Hi, glad to see there is a workaround. Tried this in an electron app and got failed after throwing an error

        var shell = require('shelljs');
        let nodePath = (shell.which('node').toString());
        shell.config.execPath = nodePath;
        shell.exec('git');

May I know is there a compatible library version to make this work? Thank you

setting exec to async seems to work.

let command = shell.exec('command', {silent:true, async:true});
command.stdout.on('data', (data) => {
    /* ... do something with data ... */
});
command.stdout.on('finish', () => {
    /* ... do something when finished ... */
});

Set it like any regular variable.

// This is inside your javascript file
var shell = require('shelljs');
shell.config.execPath = 'path/to/node/binary'; // Replace this with the real path

// The rest of your script...

Thanks to hazim-j above, I got shelljs working within electron like this:

import shell from 'shelljs';

function runCommand(command, timeout) {
  return new Promise((resolve, reject) => {
    if (timeout)
      setTimeout(() => {
        reject(new Error('Command timed out'));
      }, timeout);

    shell
      .exec(command, {
        async: true,
        silent: true
      })
      .stdout.on('data', data => {
        resolve(data);
      });
  });
}

Closing this because we’ve documented a workaround on the wiki. Please comment if this workaround is insufficient.

@ariporad Suggestion:

var nodePath = isValid(process.execPath) ? process.execPath
             : (which('node') || which('nodejs'));
if (!nodePath)
  throw new Error('Unable to find path to NodeJS executable');

Check the semantics of which() to make sure this will work.