Child Process Exec Failed Electron App Calling nodejs SpeedTest - javascript

I tried to execute a nodejs command inside electron.
const { execFile } = require('child_process');
const child = execFile('node', ['--version'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
That one above is fine! And give an output nicely.
But then once i execute :
npm install --global --save speed-test
under one of my Electron App.
And try to execute a different command such as:
const { execFile } = require('child_process');
const child = execFile('speed-test', null, (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
it gaves me an error:
Uncaught Error: spawn speed-test ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264)
at onErrorNT (internal/child_process.js:456)
at processTicksAndRejections (internal/process/task_queues.js:80)
The more interesting part is that, If i execute the command itself under CommandPrompt of Nodejs, it's perfect! So... i'm confused.
What the things that i need to modify under the Electron App anyway?

Related

BATCH: How can I close running batch file by the name using node.js

I want to know how can I close running batch file using Child process or anything in node.js
This is an example for exec in child process
const { exec, spawn } = require('child_process');
exec('my.bat', (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
In order to kill the spawned process when using 'child_process', you need to return the created ChildProcess reference, in this case by exec().
You can then kill the process using it's own API, ie. doing childProcess.kill().
Full example:
const { exec, spawn } = require('child_process');
const childProcess = exec('my.bat', (err, stdout, stderr) => {
// explicitly kill the process
childProcess.kill();
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
Find more details in the node.js docs
exec()
childProcess.kill()
If anyone is running into the issue that the batch file is not killable with .kill() or process.kill(processToKill.pid) or is starting another exe that is independent of the batch file, one solution that worked for me was to check the exe/process name I want to kill in the task manager and kill it with the (Windows) cmd command "taskkill /IM name.exe".
In code that looks like the following:
Executing the extra program (.bat, .exe, .lnk):
const systemprocess = require("child_process");
exampleVariable = systemprocess.exec("C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Accessories/Snipping Tool.lnk", (err, stdout, stderr) => {
if (err) {
console.error(err);
return;
}
console.log(stdout);
});
Killing the process:
systemprocess.exec(`taskkill /IM SnippingTool.exe`, (err, stdout, stderr) => {
if (err) {
throw err
}
if (stdout) {
console.log('stdout', stdout)
}
console.log('stderr', err)
});
Though if you execute another exe with spawn you should be able to kill it with varaibleNameOfChildProcess.kill(); like #Kworz answer suggests.
Instead of using exec use spawn, you might be able te retreive the PID generated by the bat file execution. Call process.kill(PID) to kill the running bat file
var spawn = require('child_process').spawn;
var child = spawn('my.bat', {detached: true});
process.kill(child.pid);

Spawn python ENOENT in node.js

I am creating a node js script that interacts with both python and electron js to create a GUI. I am currently stuck at an error unfortunately. That error being:
Uncaught Error: spawn python ENOENT
This is on line 167 of events.js, a script that I have not been able to find. I have currently set my PYTHON env variable to C:\Python27\python.exe and my PYTHONPATH to C:\Python27.
Here is the code that is being run when this error occurs.
function getselectors() {
var ps = require("python-shell")
var path = require("path")
var amount = document.getElementById('para').value;
var options = {
scriptPath : path.join (__dirname, '/../engine/'), args : [amount]
}
ps.PythonShell.run ('test.py', options, function (err, results) {
if (err) throw err;
swal (results [0]);
});
}

Killing a NodeJS Child Process with child.kill('SIGHUP')

Here is my code:
test.js
const {exec} = require("child_process")
var c = exec("php artisan serve", {
cwd: "C:/Users/DELL/Laravel Projects/lktest3"
}, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
})
setTimeout(() => {
c.kill('SIGHUP')
}, 10000);
When I run node test.js, I get this error:
$ node test.js
internal/child_process.js:397
throw errnoException(err, 'kill');
^
Error: kill ENOSYS
at exports._errnoException (util.js:1018:11)
at ChildProcess.kill (internal/child_process.js:397:13)
at Timeout.setTimeout (C:\Users\DELL\Documents\laravel-kit\test.js:14:7)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
I wrote this code as it is said in NodeJS Child Process API. But it's not working.
Use childProcess.spawn(command) (docs) instead of childProcess.exec, as “exec” creates a new shell and runs the command in that shell.
I used tree-kill module to kill the child process on Windows.
Use it like this:
var kill = require('tree-kill');
kill(your_child_process.pid, 'SIGKILL', function(err) {
// Do things
});

NodeJs Error: spawn C:\Windows\system32\cmd.exe; ENOENT

This is my script :
var exec = require('child_process').exec;
exec('dir', function(error, stdout, stderr) { // 'dir' is for example
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
And in the console I have :
exec error: Error: spawn C:\Windows\system32\cmd.exe; ENOENT
Someone can help me ?
This can also be caused if you are feeding in ExecOptions the options parameter, specifically 'cwd', and the path you provide is invalid
e.g:
cp.exec(<path_to_executable>, {
cwd: <path_to_desired_working_dir>
}, (err, stdout, stderr) => {
//......
})
If is not valid, the callback will be called with err equal to
Error: spawn C:\Windows\system32\cmd.exe ENOENT
I got to resolve the issue the problem is to remove the semicolon(;) from an end of the
ComSpec path C:\Windows\System32\cmd.exe
Mycomputer>properties>Advance System Settings>Environment Variables>System Variables
add this path:
ComSpec C:\Windows\System32\cmd.exe
The problem for me was that my solution directory was on a different drive than windows. Creating my solution on my C drive solved the issue.
Increasing the maxBuffer solved the problem for me.
const child_process = require('child_process');
var command = 'git ls-files . --ignored --exclude-standard --others';
child_process.execSync(command, {
maxBuffer: 1024 ** 6,
});

Node.JS: execFile ENOENT

I've tried to execute *.exe file, but got:
exec error: { Error: spawn ${__dirname}/install.exe ENOENT
Code:
var execFile = require('child_process').execFile
execFile('${__dirname}/install.exe', function(error, stderr) {
console.log('stderr: ', __dirname);
if (error !== null) {
console.log('exec error: ', error);
}
});
Also tried: '${__dirname}\install.exe', './install.exe', 'D:\install.exe'
#FelixKling has the right advice; variables don't work unless you create your string with back-ticks. Additionally, it's a good idea to use the path module to resolve file paths:
var path = require('path');
var execFile = require('child_process').execFile;
var exePath = path.resolve(__dirname, './install.exe');
execFile(exePath, function(error, stderr) {
console.log('stderr: ', __dirname);
if (error !== null) {
console.log('exec error: ', error);
}
});
Edit:
This is for your original question, about ENOENT; for your second about UNKNOWN errors, the cause can vary. It sounds like it might be a permissions issue since the executable needs to elevate to administrator permissions.

Categories

Resources