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
Related
I have an executable file that outputs every few seconds of data once i run it via the Terminal on Mac OS.
How can i get the data using node js?
Thank you.
You can run the command, which you run in the terminal, as a Node.js child process using spawn or exec.
Example:
const { spawn } = require('child_process');
const ls = spawn('ls', ['-lh', '/usr']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
ls.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
ls.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
The more information you can find here
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");
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
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.
I am using spawn() to make a git call. Sometimes it works fine but others it appears to be hanging. I see no events firing (error, exit, close) yet I see evidence that the process did in fact complete successfully.
var spawn = require('child_process').spawn;
spawn('git', ['push', 'origin', 'master'])
.on('error', function(error) {
console.log("ERROR: DETAILS: " + error);
})
.on('close', function(code) {
console.log("SUCCESS: CODE: " + code);
})
.on('exit', function(code) {
console.log("EXIT: CODE: " + code);
})
As it turns out once the stderr buffer exceeds 24kb you must be reading from it or you not see any events for completion. Possible workarounds:
Set the stdio option on the spawn call.
spawn('git', ['push', 'origin', 'master'], {stdio: 'ignore'});
See Node ChildProcess doc for all of the possibilities - there are lots.
Add an on(data) handler.
var git = spawn('git', ['push', 'origin', 'master']);
...
git.stderr.on('data', function(data) {
// do something with it
});
Pipe it to stdout / stderr. This may be too verbose for your application but including it for completeness.
var git = spawn('git', ['push', 'origin', 'master']);
...
git.stderr.pipe(process.stderr);
git.stdout.pipe(process.stdout);