Electron NodeJS spawn error when running certain commands: spawn ENOENT - javascript

So, I'm trying to use spawn in my Electron application to run the flutter --version command.
This command works perfectly fine on my terminal.
Every solution I've seen implies I don't have it in my process.env.PATH variable. But when I do a console.log( process.env.PATH );, the path to my Flutter command is there.
Here is the current code I'm trying to execute:
const flutterVer = spawn('flutter', ['--version', '/c']);
flutterVer.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
Which returns:
Uncaught Error: spawn flutter ENOENT
at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
at __node_internal_errnoException (node:internal/errors:594:12)
at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
at onErrorNT (node:internal/child_process:477:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
I tried testing with another command, like git --version, and that worked perfectly fine.

I've found a neat workaround by using exec instead of spawn like so.
const { exec } = require('child_process');
const flutterVer = exec('flutter --version');
flutterVer.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
Will have too look into what the differences are between exec and spawn, but for now, it does what I need.

Related

nodejs: valid command line prompt doesn't exec or spawn with ENOENT error

I have a bash command (debian 10, GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)):
documents=("/data/sice.pdf" "/das00/12ser.pdf");bash ./clean-pdfs.sh "${documents[*]}"
that works when I paste into terminal.
However invoking it with either exec or spawn fails without giving clear error message.
When I ran it with exec I got some complaints about the brackets. Remembering the output is quite large, I opted for spawn
const { exec } = require('child_process');
command = `documents=(${pdfPaths});` + 'bash ./clean-pdfs.sh "${documents[*]}"'
console.log(command);
const subProcess = require('child_process')
const lsChildProcess = subProcess.spawn(command)
lsChildProcess.stdout.on('data', (data) => {
console.log(data);
})
lsChildProcess.on('error', function(err) {
console.log(err);
});
and after running this nodejs script I get the following error message that isn't very helpful (i changed the paths for security reasons):
{ Error: spawn documents=("/data/Traa.pdf" "/dater.pdf");bash ./clean-pdfs.sh "${documents[*]}" ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
errno: 'ENOENT',
code: 'ENOENT',
syscall:
'spawn documents=("/daice.pdf" "/daer.pdf");bash ./clean-pdfs.sh "${documents[*]}"',
path:
'documents=("/dace.pdf" "/daer.pdf");bash ./clean-pdfs.sh "${documents[*]}"',
spawnargs: [] }
The option shell is required here to (both conditions apply):
parse multiple commands separated by ";"
run commands that are not executable, like assigning an environment variable with document=.
To have the output of the spawned process printed to the console, we can use the option stdio: 'inherit'.
Both settings are documented here: https://nodejs.org/api/child_process.html#child_processspawncommand-args-options
And here is my version of the code that I was able to test succesfully on a zsh terminal. I'm using spawnSync because it's easier to handle without a callback, but spawn works just as well.
const pdfPaths = '"/data/sice.pdf" "/das00/12ser.pdf"';
command = `documents=(${pdfPaths});` + 'bash ./clean-pdfs.sh "${documents[*]}"'
console.log(command);
const subProcess = require('child_process')
subProcess.spawnSync(command, { shell: true, stdio: 'inherit' })

node execFile python script in aws lambda

I am trying to execute a script through a node service hosted on AWS lambda, but consistently get a ENOENT exception.
2020-04-22 07:55:14.613 (-04:00) 9c8c54fc-2aa2-4d17-89d9-ca1e404191b7 ERROR Error: spawn ./bin/test-bin.py ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./bin/test-bin.py',
path: './bin/test-bin.py',
spawnargs: [ 1, 2 ],
cmd: './bin/test-bin.py 1 2'
}
Executing cat bin/test-bin.py in the child process spits out the source code of the script, ls -l through the child process shows that the script is executable, and the same code works locally on my linux machine.
const { execFile } = require('child_process');
execFile('cat', ["bin/test-bin.py"], (err, out) => {
if (err) {
console.error(err)
}
else {
console.log(out)
}
});
The script:
#!/usr/bin/python
import sys
import time
def sum(n1, n2):
return int(n1) + int(n2)
print(sum(sys.argv[1], sys.argv[2]))
Your python script is not a standalone executable - it relies on the shell interpreting the #!/usr/bin/python line at the top of the file to load the python interpreter (/usr/bin/python), and then run the script.
Because execFile doesn't load a shell, it won't do this. You could just use exec instead of execFile, but this is less safe, and slower.
Instead, run /usr/bin/python with execFile, with your script as an argument:
execFile('/usr/bin/python', ['bin/test-bin.py'], (err, out) => {
// ...
});

How to catch ENOENT error when spawning a child process in node.js on Windows, without using "shell: true"

I'm trying to create a child process, using spawn, and would like to be able to handle if the "cwd" is not found. When it's not found, it throws an "ENOENT" error, which on Windows I can only seem to catch if I spawn the process with the "shell: true" property. So what I'm doing:
const { spawn } = require('child_process')
const childProcess = spawn('MyCommand.exe', [], {
cwd: path.join(__dirname, 'mypath')
}
childProcess.on('error', (err) => {
// Handle error here
})
childProcess.on('exit', (code, signal) => {
// Handle exit event
})
On Windows, the above will not properly catch the ENOENT exception unless I do:
const childProcess = spawn('MyCommand.exe', [], {
cwd: path.join(__dirname, 'mypath'),
shell: true
}
If I spawn using the above then, if the cwd or command isn't found, the exit event will catch the exception.
But I'd really like to be able to do it without running it in a shell. Is there a way to do this?

Error: spawn npm ENOENT

I have an JS app. It works good on linux but in windows 10 I am getting an error.
events.js:161
throw er; // Unhandled 'error' event
^
Error: spawn npm ENOENT
at exports._errnoException (util.js:1028:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
at onErrorNT (internal/child_process.js:359:16)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:607:11)
at run (bootstrap_node.js:422:7)
at startup (bootstrap_node.js:143:9)
at bootstrap_node.js:537:3
and the code which is incorrect is this
const spawn = require('child_process').spawn;
const watching = [
// {service: "babel-watch"},
{service: "webpack-watch"},
// {service: "sass-watch"},
{service: "server-watch"}
];
watching.forEach(({service}) => {
const child = spawn('npm', ['run', service]);
child.stdout.on('data', d => console.log(d.toString()));
child.stderr.on('data', d => console.log(d.toString()));
});
I found the reason of this error in github I guess the problem is spawn nodejs spawn Doc which have didn't work correctly in windows. But I don't know how to modify this snippet of code to make it work. Can someone help me ?
Just changed this line
const child = spawn('npm', ['run', service]);
to this line
const child = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', service]);
Which is checking the operating system if ti's windows it runs npm.cmd if it's linux just npm
I know there is a correct answer and this question has been around for a long time, my solution is based on the answer #Armen Sanoyan and How do I determine the current operating system with Node.js
For me the #Armen Sanoyan answer does not work, but help a lot. I changed for this line and work.
const child = (process.platform === 'win32' ? 'npm.cmd' : 'npm') + ' run ' + service;
I hope I can help.
I faced the same problem. My application code was running fine in MAC but in Windows it was giving an error about the code related to the spawn command
The error occurs when running using command prompt
When I used GIT bash to start the application then the error did not occur. I did not need to change any code
I know this seems obvious but make sure you're not putting several flags/options into a single string:
// do not do this
spawn('pnpm', ['-r', 'exec', '--', 'pnpm version patch'])
// do this
spawn('pnpm', ['-r', 'exec', '--', 'pnpm', 'version', 'patch'])

Gulp build formatError - gulp + harpjs + gulp-gh-page + node

I'm working on a project based on :
https://github.com/superhighfives/charliegleason.com
Everything was working so great, and now i have a problem when i try to build the project using gulp build ...
The link to the img of the term error:
http://i.imgur.com/f1kPjLk.png
Error: 1
at formatError (/usr/local/lib/node_modules/gulp/bin/gulp.js:169:10)
at Gulp. (/usr/local/lib/node_modules/gulp/bin/gulp.js:195:15)
at Gulp.emit (events.js:107:17)
at Gulp.Orchestrator._emitTaskDone (/Users/qvoiriot/Documents/qvoiriot.github.io/node_modules/gulp/node_modules/orchestrator/index.js:264:8)
at /Users/qvoiriot/Documents/qvoiriot.github.io/node_modules/gulp/node_modules/orchestrator/index.js:275:23
at finish (/Users/qvoiriot/Documents/qvoiriot.github.io/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:21:8)
at ChildProcess.cb (/Users/qvoiriot/Documents/qvoiriot.github.io/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:29:3)
at ChildProcess.emit (events.js:129:20)
at maybeClose (child_process.js:1015:16)
at Socket. (child_process.js:1183:11)
at Socket.emit (events.js:107:17)
at Pipe.close (net.js:485:12)
The build process is the same that you can see in the link below. For the build part :
gulp.task('build', function (done) {
cp.exec('harp compile . dist', {stdio: 'inherit'})
.on('close', done)
});
If anybody have an idea about the way i can fix this ... ???
Thanks,
Q
I ran into the same error with a Gulp/Harp setup after installing nvm. I fixed it by re-installing Harp.js globally.
npm install -g harp

Categories

Resources