Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a Cli command from an Electron packaged app

I'm writing a Mac OSX GUI with Electron and Nativefier package and when i run the packaged app on a machine with Nodejs installed (globally), everything works fine.
On a machine without Nodejs installed the command can't be found.

I'm using the Nativefier Cli inside Electron with a full path to cli:

var cliCmd = '"'+app.getAppPath()+'/node_modules/nativefier/lib/cli.js"';

const child_process = require('child_process');
child_process.exec(cliCmd+' --name "App Name" "http://appname.tld" --platform darwin --arch x64 --electron-version "0.36.6"', function (error, stdout, stderr) {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

I'm getting this error: "env: node: No such file or directory"
Checked the app.getAppPath() dir and it's correct.

Also, i tried to pass the env and cwd parameters, but without any success.

child_process.exec(cliCmd+' --name "App Name" "http://appname.tld" --platform darwin --arch x64 --electron-version "0.36.6"',
{
  env: {"ATOM_SHELL_INTERNAL_RUN_AS_NODE":"1"},
  cwd: app.getAppPath()
},
function (error, stdout, stderr) {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});

What i'm doing wrong? I tried child_process.spawn() also, but without any luck again.
What i need to do to call a packaged node_module/cli from my app, on a machine without nodejs installed?

like image 657
Philip Avatar asked Jun 16 '26 08:06

Philip


1 Answers

I found execPath inside the process object and now i can execute nodejs commands on a machine without nodejs installed globally. It's using the Electron's built-in version.

child_process.exec(process.execPath+' '+cliCmd+' --name "App Name" "http://appname.tld" --platform darwin --arch x64 --electron-version "0.36.6"',
{
  env: {"ATOM_SHELL_INTERNAL_RUN_AS_NODE":"1"},
  cwd: app.getAppPath()
},
function (error, stdout, stderr) {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
});
like image 113
Philip Avatar answered Jun 18 '26 00:06

Philip