How to run exe file from node.js script? - node.js

I have an exe and I want to run it from node.js, and pass an argument, except that it doesn't work..
var exec = require('child_process').execFile;
const fs = require('fs');
try {
if (fs.existsSync("./program/bin/Release/program.exe")) {
console.log("Exists")
} else {
console.log("Not Exists")
}
} catch(err) {
console.log(err)
}
setTimeout(function() {
exec('./program/bin/Release/program.exe manual', function(err, data) {
console.log(err)
console.log(data.toString());
});
}, 0);
It definitely exists as it prints exists, and I can run it from a cmd prompt giving manual as an argument. But through node.js it is not working. It comes back with
Error: spawn ./program/bin/Release/program.exe manual ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
at onErrorNT (internal/child_process.js:456:16)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn ./program/bin/Release/program.exe manual',
path: './program/bin/Release/program.exe manual',
spawnargs: [],
cmd: './program/bin/Release/program.exe manual'
}
Does anyone know?
Thanks

Arguments should be passed as an array of strings as the second argument, like so:
exec('./program/bin/Release/program.exe', ['manual'], function(err, data) {
console.log(err)
console.log(data.toString());
});
https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

Related

imageMagick fails at socket instance

I'm using the imageMagick subclass of graphicsmagick to alter ~7k png images. The function below gets about 2/3 of the way through and fails (see log).
Seems to be an issue where a socket is failing. How do I debug this?
A thought I have is to use the -limit method, but no dice...
Thanks in advance :)
Code:
// changing an image's resolution is the same thing as changing its DPI
export default async function changeScreenshotResolution(dimension) {
new Promise(() => {
try {
const screenshots = fs.readdirSync(path.join(__dirname, `../output/screenshots/${dimension}`), function(error) {if (error) console.log(error)});
screenshots.map(screenshot => {
try {
let readStream = fs.createReadStream(path.join(__dirname, `../output/screenshots/${dimension}/${screenshot}`));
let writeStream = fs.createWriteStream(path.join(__dirname, `../output/screenshots/${dimension}1/${screenshot}`));
im(readStream, screenshot)
.limit('memory', '32MB') // i tried variations of this but it made no difference
.units('PixelsPerInch')
.density(300,300)
.stream()
.pipe(writeStream)
console.log(screenshot);
} catch (error) {
console.log(`Error changing screenshot resolution: ${error}`);
}
})
} catch (error) {
console.log(`changeScreenshotResolution error: ${error}`);
}
})
};
Error: spawn convert EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:468:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -35,
code: 'EAGAIN',
syscall: 'spawn convert',
path: 'convert',
spawnargs: [
'-density',
'300x300',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect/6550 ARABIAN CIR.png',
'-units',
'PixelsPerInch',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect1/6550 ARABIAN CIR.png'
]
}
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at tryReadStart (net.js:571:20)
at Socket._read (net.js:582:5)
at Socket.Readable.read (_stream_readable.js:474:10)
at Socket.read (net.js:622:39)
at new Socket (net.js:374:12)
at Object.Socket (net.js:265:41)
at createSocket (internal/child_process.js:313:14)
at ChildProcess.spawn (internal/child_process.js:436:23)
at Object.spawn (child_process.js:548:9)
at spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/cross-spawn/index.js:17:18)
at gm._spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:224:14)
at /Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:101:12
at series (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/array-series/index.js:11:36)
at gm._preprocess (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:177:5)
at gm.write (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:99:10)
at /Users/daddy/Dev/scout/project-sunroof-scraping/modules/changeScreenshotDPI.js:18:26
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -57,
code: 'ENOTCONN',
syscall: 'read'
}

fs.writeFile not saving even when path is correct

Having an issue with fs.writeFile in my nodejs app running locally where im getting an error of this below,
I am running on localhost xampp also on windows if that may be an issue?
[Error: ENOENT: no such file or directory, open 'C:\Users\exampleuser\Desktop\examplenodejspath\product\sku123.json'] {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\exampleuser\\Desktop\\examplenodejspath\\product\\sku123.json'
}
Below is a copy of the code.
var product = {"SKU": "sku123","name": "test"};
fs.writeFile(__dirname + "/product/" + product.SKU + ".json", product, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("product was saved!");
});
I can confirm the path is correct. but for some reason it still returns its not correct.
Any help would be appericated

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?

spawn ENOENT error with NodeJS

I get an error using GM() on my remote server, I think i get an issue when I try to write the file... It works perfectly on my local server...
My code :
gm()
.in('-page', '+0+0')
.in('/var/www/myapp/public/images/instabox.jpg')
.in('-page', '+10+10')
.in(image)
.mosaic()
.minify()
.write(newFileName, function (err) { // newfilename = '/var/www/myapp/public/uploads/mydir/image.jpg'
if (!err) console.log('done');
if (err) console.log(err);
callback();
});
My error :
{ [Error: spawn ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn' }
Response of the issue:
I put back GM on my remote server and it was the issue, GM was not
well installed...

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