admin管理员组

文章数量:1406926

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'

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'

Share Improve this question asked Jun 19, 2016 at 0:33 SrcSrc 5,5426 gold badges31 silver badges61 bronze badges 2
  • 1 Do you mean to use template literals? You have to use backticks: `${__dirname}/install.exe`. '${__dirname}/install.exe' creates a string that literally contains the character sequence ${__dirname}. – Felix Kling Commented Jun 19, 2016 at 0:34
  • @FelixKling, same again.. – Src Commented Jun 19, 2016 at 0:35
Add a ment  | 

1 Answer 1

Reset to default 5

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

本文标签: javascriptNodeJS execFile ENOENTStack Overflow