Src Src - 5 months ago 84
Node.js Question

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'

Answer

@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.

Comments