8000 exec doesn't work in electron · Issue #480 · shelljs/shelljs · GitHub
[go: up one dir, main page]

Skip to content

exec doesn't work in electron #480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jeremyrajan opened this issue Jul 24, 2016 · 38 comments
Closed

exec doesn't work in electron #480

jeremyrajan opened this issue Jul 24, 2016 · 38 comments
Labels
electron Bugs specific to the electron framework wontfix

Comments

@jeremyrajan
Copy link

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! :)

@nfischer
Copy link
Member

@ariporad can you reproduce this? This seems to be related to #471, and seems to be Mac-specific

@ariporad
Copy link
Contributor

@jeremyrajan: Can you expand the error messages to see the full stacks? Thanks!

@jeremyrajan
Copy link
Author
jeremyrajan commented Jul 29, 2016

Hi @ariporad , apologies for delay. Following is the full stack trace:

exec('node --version', {silent:true})

/Users/jr/Documents/Projects/Github/installer_app/node_modules/shelljs/src/com…:40 

exec: internal errorlog @ /Users/jr/Documents/Projects/Github/installer_app/node_modules/shelljs/src/com…:40

error @ /Users/jr/Documents/Projects/Github/installer_app/node_modules/shelljs/src/com…:68

_exec @ /Users/jr/Documents/Projects/Github/installer_app/node_modules/shelljs/src/exe…:256

(anonymous function) @ /Users/jr/Documents/Projects/Github/installer_app/node_modules/shelljs/src/com…:269

(anonymous function) @ VM345:1

Essentially, what I am trying to do get the version for a list of installed apps/packages. If there is a better way, that would be great as well 👍

Thank you :)

@jeremyrajan
Copy link
Author

Any idea guys? :). Neither of the exec commands are working for me

@nfischer
Copy link
Member
nfischer commented Aug 2, 2016

The only workaround I know might be process.version for the version. Doesn't fix this issue though.

@ariporad
Copy link
Contributor
ariporad commented Aug 4, 2016

Hmm...

We do seem to have a lot of problems with electron, don't we?

@jeremyrajan: Will you try this with the fix/480 branch, and let me know what the output is? Thanks.

ariporad added a commit that referenced this issue Aug 4, 2016
@giacgbj
Copy link
giacgbj commented Aug 5, 2016

@ariporad Do you need any further information?

Mac OS X 10.11.6
Node 6.3.1
ShellJS 0.7.0

e.message

"Command failed: "/path/to/app/node_modules/electron-prebuilt/dist/Electron.app/Contents/Frameworks/Electron Helper.app/Contents/MacOS/Electron Helper" "/var/folders/p2/v0630kzj4lb8v65d325zn5vh0000gn/T/shelljs_a4b21fa8414ec26b6caf""

e.stack

"Error: Command failed: "/path/to/app/node_modules/electron-prebuilt/dist/Electron.app/Contents/Frameworks/Electron Helper.app/Contents/MacOS/Electron Helper" "/var/folders/p2/v0630kzj4lb8v65d325zn5vh0000gn/T/shelljs_a4b21fa8414ec26b6caf"
    at checkExecSyncError (child_process.js:487:13)
    at Object.execSync (child_process.js:527:13)
    at Object.childProcess.(anonymous function) [as execSync] (ELECTRON_ASAR.js:678:28)
    at execSync (/path/to/app/app/node_modules/shelljs/src/exec.js:106:13)
    at Object._exec (/path/to/app/app/node_modules/shelljs/src/exec.js:264:14)
    at Object.exec (/path/to/app/app/node_modules/shelljs/src/common.js:275:23)
    at file:///path/to/app/app/index.html:14:15

@ariporad
Copy link
Contributor
ariporad commented Aug 5, 2016

Ohh... I think I know the problem: I think Electron is breaking process.execPath. I'll look into this a bit more.

@ariporad
Copy link
Contributor
ariporad commented Aug 5, 2016

@giacgbj: So, it would appear that the node binary is built in to the electron executable, and as such we can't run it individually. This is problematic, and I'm really not sure how we can work around it.

@nfischer
Copy link
Member
nfischer commented Aug 5, 2016

@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.

@ariporad
Copy link
Contributor
ariporad commented Aug 5, 2016

@nfischer: I'm not really sure what isValid would do. Theoretically, it should be totally valid to do:

mv /usr/bin/node /usr/bin/myNodeJSWithAFunnyName
myNodeJSWithAFunnyName script.js

@nfischer
Copy link
Member
nfischer commented Aug 5, 2016

Yes, that's the rationale behind using process.execPath. But you said execPath doesn't work, due to it being part of electron.

Just test if execPath points to something that is part of electron, then fallback to a name we know node usually goes by. If process.execPath doesn't work, and neither do any of the usually names for the node binary, then we can throw an error. It won't handle every case, but wouldn't it fix this issue?

@ariporad
Copy link
Contributor
ariporad commented Aug 6, 2016

@nfischer: There would be no way to test if it's part of electron. For example, Slack is written with Electron. From Slack's perspective, process.execPath is /Applications/Slack.app/Contents/MacOS/Slack. Atom is also written with slack. From Atom's perspective, process.execPath is /Applications/Atom.app/Contents/MacOS/Atom. So, I'm not sure how we would detect this.

I don't think we could just test if the path ends in node, because that would break for iojs, (or if there are symlinks).

We could have a shell.config.electron, which feels messy. Or, we could not support electron, which I don't like. Or, we could try to detect electron and do something special based on it. (Maybe we could submit a PR to electron which adds a -e option?).

Another thing is that Electron apps are frequently non-developer facing, so it might be bad to require people to have a seperate node instance on their machines.

@nfischer
Copy link
Member
nfischer commented Aug 6, 2016

@ariporad perhaps electron/electron#2288 would help us? That'll tell us if we're running in electron. If so, we know that process.execPath won't work for us.

@giacgbj
Copy link
giacgbj commented Aug 8, 2016

Is there a reason you can't simply call exec/execSync from node's child_process?

@ariporad
Copy link
Contributor
ariporad commented Aug 8, 2016

Hmm... @giacgbj, Good point. @jeremyrajan?

@jeremyrajan
Copy link
Author

hmm, that works too but is it cross platform compatible because I believe normally we have to execute a bat file for WIN? Or am i totally wrong :)

@nfischer
Copy link
Member
nfischer commented Aug 9, 2016

@jeremyrajan

var ret = child_process.execSync('node --version');

What's the issue with Windows?

@nfischer
Copy link
Member
nfischer commented Jan 9, 2017

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

@nfischer nfischer closed this as completed Jan 9, 2017
@HardikDG
Copy link
HardikDG commented Aug 6, 2017

As per wiki I need to set 'config.execPath', How can I specify that? Any sample example for it?

@nfischer
Copy link
Member
nfischer commented Aug 6, 2017

@HardikDG please see the wiki, it explains how you might find the path to your node binary...

@HardikDG
Copy link
HardikDG commented Aug 7, 2017

@nfischer I already saw the wiki, from the wiki itself I come to this post, I got the path to the node in my system.
My question is where I need to set this path? it says 'set config.execPath' but where? In my JS file or in my system where?
Can you please show the command/code to set this path

Sorry if it's very stupid question but I don't have deep knowledge of NodeJS, so I don't have that idea.

@nfischer
Copy link
Member
nfischer commented Aug 8, 2017

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...

@HardikDG
Copy link
HardikDG commented Aug 8, 2017

@nfischer Thanks. It worked in my electron app.

@rustinpython
Copy link
rustinpython commented May 10, 2018

@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

@nfischer
Copy link
Member

@gayanai does that workaround not work for you? This is still hit-or-miss, and we don't guarantee that workaround (otherwise, we would bake it into shelljs core).

@nfischer nfischer changed the title exec doesnt seem to be working exec doesn't work in electron May 10, 2018
@rustinpython
Copy link

@nfischer Hi , the workaround not works um using the latest macos environment with the latest electron framework. Thanks

@nfischer
Copy link
Member

@gayanai well, where is your path to node? For example, run this in the terminal:

$ which node || which nodejs # does this point to something?

Whatever that points to should be your execPath value. This is a best-effort kind of thing. If you don't have a standalone installation of node outside of electron, you'll need to install one for exec() to work.

@rustinpython
Copy link

@nfischer Hi
The path is /usr/local/bin/node and I have referred to that path

@nfischer
Copy link
Member

@gayanai if that doesn't fix the issue, then you'll need to provide full error output

@hazim-j
Copy link
hazim-j commented Jun 27, 2018

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 ... */
});

@nfischer
Copy link
Member

@hazim-j yes, that is another valid workaround (assuming async works for your use-case). Now that we have async/await in some versions of node, maybe this is a better workaround.

@nanjixiong218
Copy link

node: 10.9.0

electron: 2.0.6

shelljs: 0.8.2

I have set the shell.config.execPath:

import shell from 'shelljs';
shell.config.execPath = '/Users/xuhuiyuan/.nvm/versions/node/v10.9.0/bin/node';

but it product another problem:

image

@nfischer
Copy link
Member

@nanjixiong218 thanks for reporting. Yes, it seems this isn't quite working. I'm not sure what that file (shelljs_22ff...) is supposed to be, so I'm not sure what options to recommend.

@DrakeXiang
Copy link

shell.witch('node') is null after package with electron-forge, then I used child_process instead

@tracker1
Copy link

Forgive my ignorance... If you're bundling the app, couldn't you create an npm dependency to node@compatible-version and reference that?

@nfischer
Copy link
Member

If you're bundling the app, couldn't you create an npm dependency to node@compatible-version and reference that?

If you're requesting that ShellJS bundle node as a dependency, that would blow up our module's size. If you're recommending this for authors of electron apps, then this might be reasonable? I don't know enough about electron to comment, but it otherwise seems like it would satisfy ShellJS's requirements.

@zxc23
Copy link
B04A zxc23 commented Aug 25, 2019

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);
      });
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
electron Bugs specific to the electron framework wontfix
Projects
None yet
Development

No branches or pull requests

0