child_process.exec not finding cmd in path - node.js

So I have installed ffmpeg and added it into my path.
If I run ffmpeg in cmd.exe, it runs ffmpeg.exe.
But when I use this:
const { exec } = require('child_process');
await asyncExec(`ffmpeg -i ${filename}%05d.png ${aviname}`);
//Note: the variables are filled in
I receive this error:
Command failed: ffmpeg -i thing%05d.png thing.avi
'ffmpeg' is not recognized as an internal or external command,\r\noperable program or batch file.
I do not understand why this happpens. I'm just running this app in VSCode debugging.
Edit: asyncExec is this function:
const asyncExec = text => new Promise((res, rej) => {
exec(text, (err, result) => {
if (err) {
rej(err);
} else {
res(result);
}
});
});

After restarting Visual Studio Code it works again. Weird.

Related

How to run function in child process in Node.js

I've been able to successfully run commands using the exec() command. However, I'd like to leave a process running and continue to run commands on the open process, then close on app exit. Take this generic code:
const { exec } = require("child_process");
exec("XR_Command -i 192.168.0.100 -f /ch/01/on | kill", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
I would like to keep the XR_Command process active so that I can issue commands to the active process. So, basically I would like to do this:
> XR_Command -i 192.168.0.100
> /ch/01/on
> /ch/02/on
> /ch/03/on
I cannot for the life of me figure out how to make this function properly by referencing the existing child process. Thanks!
Okay, so after a day I figured out two main problems I was running in to, here is my working code:
const { spawn } = require('child_process');
let Command = spawn('X_Control', ['-i', '192.168.0.1']);
Command.stdout.pipe(process.stdout);
Command.stderr.pipe(process.stderr);
Command.stdin.write('some command\n');
Command.on('error', function(err) {
console.error(err);
});
Command.stderr.on('data', (data) => {
console.log(`stderr: ${data}`);
});
Command.on('close', (code) => {
console.log(`child process exited with code ${code}`);
});
Issue 1: My application command was X_Control -i 192.168.0.1, every space needs to be quoted separately as Command = spawn('X_Control', ['-i', '192.168.0.1']); This took me a while to track down.
Issue 2: Command.stdin.write('some command\n'); is how I execute commands on my running application, and it must be followed by \n in order to execute the command.

How to store output of a shell command in nodejs and use it for other operation

I am trying to get a helm release name via executing below code in nodejs
and then wanted to delete that release
var sys = require('sys')
var spawn = require('child_process').spawn;
output = spawn('helm',['list', '-q', '--namespace', 'd35nb8']);
release = output.stdout.on('data', (data) => {
var test = process.stdout.write(data.toString());
process.stdout.write(data.toString())
spawn('helm',['delete', test]);
});
code here is able to get the helm release name but could not delete the release
code outputs as
oot#5a857d30a4c1:/opt/api# nodejs test2.js
inside moving further
(node:2272) [DEP0025] DeprecationWarning: sys is deprecated. Use util instead.
kilted-markhor
kilted-markhor
how could I achieve this logic here in nodejs
Usually, spawn is needed for more sophisticated child process management. For the described use case I would suggest using simple exec:
const exec = require('child_process').exec;
exec('helm list -q --namespace d35nb8'], (err, stdout, stderr) => {
if (err) {
console.log('helm list failed', err);
} else {
const releases = stdout.split('\n'); // or whatever is the separator
for (const r of releases) {
console.log('deleting release', r);
exec('helm delete ' + r, (err2) => {
if (err2) {
console.log('helm delete failed', err2);
}
});
}
}
});

Run Bash-Script within Electron App using child_process.exec

I'm struggling with running a bash-script within main.html.
const exec = require("child_process").exec;
// Execute bash script
exec("/c/workspace/_edu_zone/Proxy_Manager/filemover.sh", shellCallback);
// Callback
function shellCallback(error, stdout, stderr) {
console.log(error, stdout)
}
I'm always getting the error: no such file or directory. What am i doing wrong?
Any help is highly appreciated.
change
/c/workspace/_edu_zone/Proxy_Manager/filemover.sh
to
c:/workspace/_edu_zone/Proxy_Manager/filemover.sh
or
your could try using node-powershell to execute the command directly
const shell = require('node-powershell')
let ps = new shell({
executionPolicy: 'Bypass',
noProfile: true
});
function lunchnode() {
process.stdout.write('logging');
ps.addCommand('node run.js')
ps.invoke()
.then(function (output) {
process.stdout.write(output)
}).catch(function (err) {
process.stdout.write(err)
ps.dispose()
})
}

NodeJS exec() command for both Windows and Ubuntu

Using NodeJS, NPM, and Gulp.
I want to build a gulp task to run JSDoc that works on Ubuntu and Windows.
This works on Ubuntu...
var exec = require('child_process').exec;
return function(cb) {
exec('node node_modules/.bin/jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
And this works on Windows...
var exec = require('child_process').exec;
return function(cb) {
exec('node_modules\\.bin\\jsdoc -c jsdoc-conf.json', function(err, stdout, stderr) {
cb(err);
});
};
Needless to say, neither works on the other. How do others solve this type of problem?
Try using path.resolve, which should provide you with a full path to the file regardless of the platform.
Node has process.platform, which... "returns a string identifying the operating system platform on which the Node.js process is running. For instance darwin, freebsd, linux, sunos or win32"
https://nodejs.org/api/process.html#process_process_platform
var exec = require('child_process').exec;
return function(cb) {
if (process.platform === 'win32') {
// Windows OS
} else {
// everything else
}
};
Using path.resolve:
const exec = require('child_process').exec;
const path = require('path');
return function(cb) {
let command = `node ${path.resolve('node_modules/.bin/jsdoc')} -c jsdoc-conf.json`;
exec(command, function(err, stdout, stderr) {
cb(err);
});
};

Enumerate system drives in nodejs

Is there a way to retrieve the drive name of all logical drives on a computer ?
I've looked at the fs api, but from there I can only enumerate the files and directories of a given directory.
I'm not sure what you mean by "drive name". If you mean drives in the form of \\.\PhysicalDriveN, I faced the same problem and implemented this module that works in all major operating systems:
https://github.com/resin-io/drivelist
For Windows, you get information such as:
[
{
device: '\\\\.\\PHYSICALDRIVE0',
description: 'WDC WD10JPVX-75JC3T0',
size: '1000 GB'
},
{
device: '\\\\.\\PHYSICALDRIVE1',
description: 'Generic STORAGE DEVICE USB Device',
size: '15 GB'
}
]
If you targeting on Windows, you could try this:
This solution base upon the idea from this post.
I wrap it with promise.
var spawn = require("child_process").spawn
function listDrives(){
const list = spawn('cmd');
return new Promise((resolve, reject) => {
list.stdout.on('data', function (data) {
// console.log('stdout: ' + String(data));
const output = String(data)
const out = output.split("\r\n").map(e=>e.trim()).filter(e=>e!="")
if (out[0]==="Name"){
resolve(out.slice(1))
}
// console.log("stdoutput:", out)
});
list.stderr.on('data', function (data) {
// console.log('stderr: ' + data);
});
list.on('exit', function (code) {
console.log('child process exited with code ' + code);
if (code !== 0){
reject(code)
}
});
list.stdin.write('wmic logicaldisk get name\n');
list.stdin.end();
})
}
listDrives().then((data) => console.log(data))
Test it, you will see the result like:
["c:", "d:"]
Based on Edwin Lees answer:
const child = require('child_process');
child.exec('wmic logicaldisk get name', (error, stdout) => {
console.log(
stdout.split('\r\r\n')
.filter(value => /[A-Za-z]:/.test(value))
.map(value => value.trim())
);
});
Output: ['C:', 'D:'] etc.
How about using the DiskPart command? Does running diskpart list in the command line give you the output you need? If so you can execute this in node using child_process.exec
var exec = require('child_process').exec
var cmd = 'diskpart list'
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.log('error running diskpart list command')
console.log(err)
return
}
console.log('stdout data')
console.log(stdout)
console.log('stderr data')
console.log(stderr)
})
+1 for #Bagherani's downgrade suggestion!
I am using Electron React Boilerplate v4.0 and could not get drivelist to load. I downgraded to drivelist#5.2.12 and it works for my needs.

Resources