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.
Related
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?
I have a file that I am trying to write to from a post. Currently, the FS does not error nor does it write to file. However, when taking the same code from the deployed build and running it locally, it works. I even kept the file path consistent since it was throwing no permissions error at first. I ensured this file wrote to the same directory so each Filewrite Stream process would look at the same directory and file.
Local build:
var fs = require('fs');
const path = require('path');
var user_name = 'Password';
var password = 'test';
var errSTR = ""
fs.writeFile('C:\\hi.txt', 'Content to write', { flag: 'w' }, function(err) {
if (err)
return console.error(err);
fs.readFile('C:\\hi.txt', 'utf-8', function (err, data) {
if (err)
return console.error(err);
console.log(data);
});
});
Deployed Build:
app.route('/test')
.get(function(req, res) {
res.send('GET test');
})
.post(function(req, res) { // Start Post
var boolTry = false;
try {
boolTry = true;
var bool = false
var user_name = "Password"//req.body.user;
var password = "test"//req.body.password;
var errSTR = ""
fs.writeFile('C:\\hi.txt', user_name + password, { flag: 'w' }, function(err) {
if (err)
return console.error(err);
fs.readFile('C:\\hi.txt', 'utf-8', function (err, data) {
if (err)
return console.error(err);
res.send(500 + err);
console.log(data);
});
})
} catch (error) {
bool = true
errSTR = error
}
res.send('POST test' + " " + boolTry + " " + bool + " " + errSTR + ";")
})//END POST
.put(function(req, res) {
res.send('PUT test');
});
The local build will properly write to the file, while the dev build appears to do nothing. It should be noted by booleans were being used to understand how the file writer works but here is the server response from build: successful response POST test true false ;
Using:
IISNODE for iis: 7.x
Express: 4.16.2
node.js: v8.9.4
cors: 2.8.4
body-parser: 1.17.2
Sidenote: If you are confused by the writing portion of code, the intention was to write, check error then read, check error for assurance.
Update
Reoccurring error based on certain filewrite methods Error: EPERM: operation not permitted, open. Yes, all permissions for the directory are enabled along with ensuring read and write are checked.
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,
});
I am using Node JS with Express and trying to execute a script and return the output of that script to the client via AJAX. The script is completing successfully, but for some reason I cannot get the output to show in the post response.
let childProcess = require('child_process');
router.post('/update', (req, res) => {
childProcess.exec('/home/dir/app/update.sh', { shell: '/bin/bash' }, (error, stdout, stderr) => {
res.json({ error, stdout, stderr });
});
});
The Node process is run using Forever. If I look at the forever log, it shows:
Forever detected script was killed by signal: SIGKILL
Not sure what that means. It appears the script is completing successfully though.
EDIT
To address Aikon's answer below. I tried the following and still no go.
router.post('/update', (req, res) => {
console.log('start...');
childProcess.exec('/home/dir/app/update.sh', { shell: '/bin/bash' }, (error, stdout, stderr) => {
console.log('done');
error = error || '';
stdout = stdout || '';
stderr = stderr || '';
res.json({ error, stdout, stderr });
});
});
It's as if the success function is never firing because it never logs "done" in the console. It just logs "start..." and the SIGKILL error above in the console.
Your script kill(and restart) itself before it can read output from child process.
Look at your update.sh again:
#!/bin/bash
git pull
npm install
npm run build
#this command restarts your script
forever restartall
You could remove last line of update.sh, and after sending response, the script just exits, forever should restart it with updated version.
router.post('/update', (req, res) => {
childProcess.exec('/home/dir/app/update.sh', { shell: '/bin/bash' },
(error, stdout, stderr) => {
res.json({ error, stdout, stderr });
process.exit();
});
});
If error is not empty then output is undefined => fail
After stringify error is empty object.
'use strict';
let error = new Error('err');
let str = 'text';
let obj = {a: 10, b: 15}; // Try comment to get fall
console.log(JSON.stringify({error, str, obj}))
You have a syntax error in your code. Try:
res.json({ error: error, output: stdout, error_log: stderr });
alternatively you can do:
res.json([ error, stdout, stderr ]);
How to create DB, using mongojs?
I tried to run command, but there are errors...
var db = require('mongojs').connect('learn', ['mikecollection']),
mycollection = db.collection('mikecollection'),
query_results,
data = require('./data.json'),
sys = require('sys');
var util = require('util'),
exec = require('child_process').exec,
child;
child = exec('mongoimport --db mikeDB --collection mycol --file "A:\Users\Mike\Desktop\nodejs\2_temp\data.json"',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
Error:
Error: Command failed: 'mongoimport' is not recognized as an internal or external command –