node failed exit code 1 - node.js

I am trying to run photoshop script through childprocess execsync using the following code, But am getting a error
try {
const child_process = require("child_process")
child_process.execSync('"C:/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe" Z:/myfile.jsx', (err, stdout, stderr) => {
if (err) {
console.log("error here");
}
})
.on('close', function (code, signal) {console.log('go to next step')})
} catch (err) {
err.stdout;
err.stderr;
err.pid;
err.signal;
err.status;
}
But I am getting a error
{ Error: Command failed: "C:/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe" Z:/Render2.0/psTextConvertor.jsx
at checkExecSyncError (child_process.js:611:11)
at Object.execSync (child_process.js:648:13)
at startPhotoshop (Z:\Render2.0\tempsqs.js:422:23)
at mergeFontData (Z:\Render2.0\tempsqs.js:410:9)
at ChildProcess.<anonymous> (Z:\Render2.0\tempsqs.js:282:21)
at ChildProcess.emit (events.js:187:15)
at ChildProcess.EventEmitter.emit (domain.js:442:20)
at maybeClose (internal/child_process.js:962:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
status: 1,
signal: null,
output: [ null, <Buffer >, <Buffer > ],
pid: 5492,
stdout: <Buffer >,
stderr: <Buffer > }
but the script runs perfectly , but I am not getting exit code , please help

I had similar problems getting Photoshop to launch and run a script from node as a child process. I was working on a Windows system with Adobe CS6 at the time and was able to get this working by writing a powershell script to launch photoshop with args to execute an ExtendScript file, and then used child-process.exec to run that powershell script from node.
powershell:
Start-Process -FilePath "C:\Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Photoshop.exe" -ArgumentList "C:\Scripts\MyScript.jsx" -Wait
node:
function runPowershell(path) {
var spawn = require("child_process").exec, child;
child = spawn(`powershell -ExecutionPolicy Bypass -File ${path} -NoExit`);
child.stdout.on("data", function (data) {
console.log("Powershell Data: " + data);
});
child.stderr.on("data", function (data) {
console.log("Powershell Errors: " + data);
});
child.on("exit", function () {
console.log("Powershell Script finished");
});
child.stdin.end(); //end input
}
runPowershell("path/to/powershellScript.ps1");

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

child_process.spawn ENOENT error

I'm have some function like this
function startMain(){
child_process.spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install'], {
cwd : somedir,
stdio : 'inherit'
}).on('close',()=>{
child_process.spawn('node', ['app'], {
cwd : somedir,
stdio : 'inherit'
}).on('error', (err)=>{
console.log('Error occured during starting. Aborting...\nError stack: Cannot exec node\n');
throw(err); // something wrong with spawn
});
}).on('error', (err)=>{
console.log('Error occured during starting. Aborting...\nError stack: Cannot exec npm\n');
throw(err); // something wrong with spawn
});
}
On Windows, Linux and Android machines I'm have the same error: ENOENT while trying to execute npm. It's present in process.env.PATH. What I'm doing wrong?
P.S. somedir is defined and equal to an existing directory
Sorry, guys, it's my fault. somedir does NOT really exists. Closing the question

How to run a windows batch file with node js

What I have tried: Using Node JS Child Process API to spawn the bat file in my projectfolder. But I couldnt get it running.
projectfolder/
├──src/
| ├──app
| ├──home
| ├──home.component.ts
|── my.bat (File Type: Bat shortcut)
home.component.ts
let spawn = require('child_process').spawn,
ls = spawn('cmd.exe', ['/c', 'my.bat']);
ls.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
ls.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
ls.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
Error
stderr: 'my.bat' is not recognized as an internal or external command,
operable program or batch file.
home.component.ts?71c4:159 child process exited with code 1
I have also tried
const exec = require('child_process').execFile;
exec('my.bat', function (err, data) {
console.log(err);
console.log(data);
});
with error
Error: spawn my.bat ENOENT
at exports._errnoException (util.js:1050)
at Process.ChildProcess._handle.onexit (internal/child_process.js:193)
at onErrorNT (internal/child_process.js:367)
at _combinedTickCallback (internal/process/next_tick.js:80)
at process._tickCallback (internal/process/next_tick.js:104)
Update: I found the issue.
My File Type is a Bat shortcut
The above code only works for Windows Batch File, but not a shortcut

Windows Fails to recognize installed commands while using child_process.exec() in nodejs

I am able to run 'ipconfig.exe' or 'netstat.exe' using exec but I am unable to run .bat command "chef" or "chef-apply".
OS: Windows 7
exports.testscript=function(req,res){
var exec = require('child_process').exec;
console.log("inside function");
var child = exec('chef-apply azurepro.rb' , {cwd :'C:\Users\xyz\chef-repo'},
function(error, stdout, stderr){
console.log(stdout);
console.log(stderr);
if (error !== null) {
console.log(error);
return;
}
});
//child.stdin.end();
};
I am getting this error,
{ [Error: spawn cmd.exe ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn cmd.exe',
path: 'cmd.exe',
cmd: 'cmd.exe /s /c "chef-apply azurepro.rb"' }
From my understanding the bin path for command is not resolved. How can I fix this?

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.

Resources