Error: spawn npm ENOENT - javascript

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'])

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

Electron NodeJS spawn error when running certain commands: spawn ENOENT

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.

Getting error on gatsby build command: TypeError: Unexpected response from worker: undefined

I am getting this error when I run the command $ gatsby build on the terminal.
What gets my attention on this one is that it mentions jest-worker, and I don't see that module on my package.json. I see it only in the node_modules folder.
I already tried many things. Deleting node_modules, package_lock.json, npm cache clean --force and some other things.
I can't find these errors on the internet either.
If I keep scrolling down on the terminal, the error ends with this:
/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/yoga-layout-prebuilt/yoga-layout/build/Release/nbind.js:53
throw ex;
^
Error: write EPIPE
at process.target._send (internal/child_process.js:806:20)
at process.target.send (internal/child_process.js:676:19)
at /Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/loggers/ipc/index.js:58:13
at dispatch (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/redux/index.js:54:5)
at Object.createLog (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/redux/lib/redux.js:483:12)
at Reporter.log (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/reporter.js:173:40)
at Object.console.log (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby-cli/lib/reporter/patch-console.js:17:14)
at WithHeaderLayout (/Users/marcelo/Work/SM/gatsby-on-demand/public/render-page.js:104712:376)
at d (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:36:498)
at $a (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:39:16)
at a.b.render (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:476)
at a.b.read (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:44:18)
at renderToString (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/react-dom/cjs/react-dom-server.node.production.min.js:54:364)
at Module.default (/Users/marcelo/Work/SM/gatsby-on-demand/public/render-page.js:710:28)
at /Users/marcelo/Work/SM/gatsby-on-demand/node_modules/gatsby/dist/utils/worker/render-html.js:28:36
at Promise._execute (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/bluebird/js/release/debuggability.js:384:9)
Emitted 'error' event on process instance at:
at processEmit [as emit] (/Users/marcelo/Work/SM/gatsby-on-demand/node_modules/signal-exit/index.js:161:32)
at internal/child_process.js:810:39
at processTicksAndRejections (internal/process/task_queues.js:75:11) {
errno: 'EPIPE',
code: 'EPIPE',
syscall: 'write'
}
I've been going through this the whole day.
Any ideas?

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

Running a Grunt Task directly from Node

How do I execute a Grunt task directly from Node without shelling out to the CLI?
I've got the following "POC" code; however, "stuff" is never logged.
var grunt = require('grunt');
grunt.registerTask('default', 'Log some stuff.', function() {
console.log('stuff');
});
grunt.task.run('default'); // This is probably not the right command
I'm pretty new to Grunt, so I'm probably missing something obvious. I suspect the command I'm using to "run" the task is just queuing it, and doesn't actually start running things. I can't find documentation for manually running things, though.
Update
While this is the answer, Grunt has tons of issues with being run directly from Node. Not the least of which is when the grunt task fails, it calls process.exit and nicely quits your node instance. I cannot recommend trying to get this to work.
Ok, I'll just answer my own question. I was right, the command I had was wrong.
Updated code:
var grunt = require('grunt');
grunt.registerTask('default', 'Log some stuff.', function() {
console.log('stuff');
});
grunt.tasks(['default']);
it take much time and finally, I already made it working for me
var util = require('util')
var exec = require('child_process').exec;
var child = exec("/usr/local/bin/grunt --gruntfile /path/to/Gruntfile.js", function (error, stdout, stderr) {
util.print('stdout: ' + stdout);
util.print('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
What if you do the following?
grunt.tasks("default");
I've created an Grunt runner in one of my projects that does some parsing and then call the line above. Almostly what you already answered, but with support for a Gruntfile.js.
We're using Jenkins for our builds. So here is how we resolved the issue using bash:
#!/bin/bash
export PATH=$PATH:/usr/local/bin
grunt full-build | tee /dev/stderr | awk '/Aborted/ || /Fatal/{exit 1}'
echo rv: $?
exit $?
The use of /dev/stderr is because we're running within Jenkins and we still want to ouput to show up within the console.
Visualjeff

Categories

Resources