shell script on node.js - node.js

I am very new to node.js,
I am trying to create a node.js script with the execution of shell script in it.
Here is the code which i have .
var spawn = require('child_process').spawn
var _ = require('underscore');
var deploySh = spawn('sh', [ 'vij.sh' ], {
cwd: process.env.HOME + '/u/qa/gv/node/scripts',
env:_.extend(process.env, { PATH: process.env.PATH + ':/usr/local/bin' })
});
and when i try to execute it, i am facing the below issue. Can anyone help me on this?
node vijay
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:988:11)
at Process.ChildProcess._handle.onexit (child_process.js:779:34)

spawn is complaining that it can't find 'sh', use 'bash' instead (you might also need to specify the full path to your script depending on your env setup.)
so I'd try:
spawn('bash', ['vij.sh'], ...
spawn('bash', ['/my/path/to/vij.sh'], ...
spawn('/my/path/to/vij.sh', [], ...

var spawn = require('child_process').spawn
var _ = require('underscore');
var deploySh = spawn('sh', [ 'vij.sh' ], {
// cwd: process.env.HOME + '/u/qa/gv/node/scripts',
env:_.extend(process.env, { PATH: process.env.PATH + ':/usr/local/bin' })
});
Comment cwd: line no. 4
Until now I don't know what is process.env.HOME value but this worked for me.

Related

What does error code 255 mean in Node.js child_process

I am running php command from Node.js child_process.
In response it is showing error and throwing error with error code 255 because of which my script is breaking.
When i execute the command from terminal it works fine but in child_process it gives error.
async function decrypt(cipher){
try {
var terminal = require('child_process');
var util = require('util');
var exec = util.promisify(terminal.exec);
var command = "usr/bin/php " + basePath + "/decrypt.php '" + cipher +"'";
return await exec(command);
}catch(err){
console.log(err);
console.error(err.code +' | '+ err.message);
}
}
module.exports = {
decrypt : decrypt
}
{ Error: Command failed: /usr/bin/php /var/www/html/nodejs/decrypt.php 'w5oFS8U4NGrleFHtptkmO+luDMw0z+fYrJ/onlj6fndIS/0QrWzOAsTN450VLZwR+GmDkyylybGJXUnx2VdeJQ=='
at ChildProcess.exithandler (child_process.js:281:12)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at maybeClose (internal/child_process.js:915:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
killed: false,
code: 255,
signal: null,
cmd: '/usr/bin/php /var/www/html/nodejs/decrypt.php \'w5oFS8U4NGrleFHtptkmO+luDMw0z+fYrJ/onlj6fndIS/0QrWzOAsTN450VLZwR+GmDkyylybGJXUnx2VdeJQ==\'',
stdout: '\t\n\n{"error":false,"decryptedText":"0324"}',
stderr: '' }
Desired Output is : '{"error":false,"decryptedText":"0324"}'
The error trace stack is more important than the error code.
process exit code

NodeJs script with child_process spawn on Windows, why I need 'shell: true' for ENOENT error?

I'm using this code:
const {
spawn
} = require('child_process');
let info = spawn('npm', ["-v"]);
info.on('close', () => {
console.log('closed');
}
But I have this error:
events.js:182
throw er; // Unhandled 'error' event
^
Error: spawn npm ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
at onErrorNT (internal/child_process.js:366:16)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
at Function.Module.runMain (module.js:607:11)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
If I use instead:
let info = spawn('npm', ["-v"], {shell: true});
it works!
But why I need shell: true? I need also to see the stdout of that spawn, so I'm also using this:
let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'});
It's correct?
While calling spawn itself, there is no npm command under spawn. Thus you got that error message. Instead of using spawn itself, while adding shell: true, spawn will use shell of your system to run that command. Since your system has npm, it works.
let info = spawn('npm', ["-v"], {shell: true, stdio: 'inherit'}); It's correct?
The code is fine if your parameters of spawn are controllable. But generally, I suggest use pure spawn without using shell. The risk will reduce without touching shell directly.
Since you need the stream return from spawn. I have checked other solution here. Without shell: true, You can use the code:
const {
spawn
} = require('child_process');
let projectPath = ''//the path of your project
let info = spawn('npm', ['-v'], { cwd: projectPath });
let result = '';
info.stdout.on('data', function(data) {
result += data.toString();
console.log(result);
}

Node.js ENOENT Reading PDF file

I need to read pdf file and I use pdf-text-extract. It works perfectly on my localhost. But when I tried to run the program on server, I got the following error
spawn called
{ '0': 'pdftotext',
'1':
[ '-layout',
'-enc',
'UTF-8',
'/tmp/the_file_name.pdf',
'-' ],
'2': { encoding: 'UTF-8', layout: 'layout', splitPages: true } }
events.js:72
throw er; // Unhandled 'error' event
Error: spawn ENOENT
at errnoException (child_process.js:1011:11)
at Process.ChildProcess._handle.onexit (child_process.js:802:34)
Here is how I use pdf-text-extract
var extract = require('pdf-text-extract');
.....
.then (function () {
console.log(fs.readdirSync('/tmp'));
var extractAsync = Promise.promisify(extract);
return extractAsync(filePath);
})
.catch (function (err) {
console.log(err);
});
As you can see, I have added catch, but why the error is Unhandled 'error' event.
I have also checked that the file is exist using fs.readdirSync. What cause the error and how can I fix it?
Your server does not have the pdftotext command, which the pdf-text-extract module tries to spawn as a child process. The readme for the module includes a link to how to install the program for various platforms.

spawning a child process to call npm-installed command in nodejs causes ENOENT in windows

I've searched a lot but got no correct answer.
Firstly I'm sure the command is usable under command line, here is the output:
> lessc
lessc: no input files
usage: lessc [option option=parameter ...] <source> [destination]
However when use child_process.spawn, I got:
> node test.js
Encountered error: { [Error: spawn ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn' }
I'm sure the process.env is given to spawn, here is the nodejs code:
var build = require('child_process').spawn(
'lessc',
[],
{
stdio: 'inherit',
env: process.env
}
);
build.on(
'error',
function (err) {
console.log('Encountered error:', err);
process.exit();
}
);
build.on(
'close',
function (err) {
console.log('close');
}
);
And weiredly, it only encounter ENOENT when the command is installed via npm install -g, it works well on for example dir or del system command
As is turns out, the following works:
var spawn = require('child_process').spawn;
var b = spawn(
process.env.comspec,
['/c', 'lessc'],
{ stdio: 'inherit' }
);
Note that you don't need to explicitly pass env as you do, the default is to inherit.

Forever-monitor throwing ENOENT and not working

so I have:
var forever = require('forever-monitor');
var Monitor = forever.Monitor;
var child = new Monitor('clusters.js', {
max: 10,
silent: false,
killTree: true,
logFile: './logs/forever.log',
outFile: './logs/app.log',
errFile: './logs/error.log'
});
child.on('exit', function (err) {
console.log('Server exitted');
});
child.start();
and it always throw the same error: events.js:72 throw er; // Unhandled 'error' event with:
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
npm ERR! weird error 8
npm ERR! not ok code 0
Does anyone know what is going on and how to fix it?
Im on Windows 7 with:
"express": "3.3.5",
"forever-monitor": "~1.2.2"
https://github.com/blai/grunt-express/issues/12
Apparently the problem is with forever-monitor 1.2, I downgraded to 1.1 and it just worked.
From what I got there they dont seem to be doing anything about it either...

Resources