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

9
这是我的脚本:
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}`);
    });

在控制台中,我看到:

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

有人可以帮我吗?

听起来像是Node没有PATH或没有权限运行cmd.exe。它正在尝试为您运行cmd.exe(因为您提供了内置的dir,它很聪明),但它找不到它(要么是因为它真的不存在,要么是操作系统在拒绝访问时告诉它的)。 - T.J. Crowder
非常感谢 @T.J.Crowder,我会继续按照您的建议进行搜索 :) - Jiohn Dioe
@T.J.Crowder 我也有这个问题: { [Error: spawn C:\Windows\system32\cmd.exe; ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn C:\Windows\system32\cmd.exe;', path: 'C:\Windows\system32\cmd.exe;', spawnargs: [ '/s', '/c', '"dir"' ], cmd: 'C:\Windows\system32\cmd.exe; /s /c "dir"' }你看到别的问题了吗? - Jiohn Dioe
我找到了一个解决方法,请查看我的答案: https://stackoverflow.com/questions/40798047/uncaught-error-spawn-node-exe-enoent/45275282#45275282 - Thomas
在Windows 7上安装构建工具窗口可以解决问题,https://github.com/felixrieseberg/windows-build-tools#examples-of-modules-supported - vikas etagi
4个回答

17
如果您在ExecOptions的选项参数中提供了'cwd'并且提供的路径无效,则也可能会导致这种情况发生。
例如:
cp.exec(<path_to_executable>, {
  cwd: <path_to_desired_working_dir>
}, (err, stdout, stderr) => {
  //......
})

如果不合法,回调函数将被调用并出现错误:

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


这对我解决了问题,非常感谢。 - Liam

10

我需要解决的问题是从ComSpec路径C:\Windows\System32\cmd.exe末尾删除分号(;)

我的电脑>属性>高级系统设置>环境变量>系统变量

添加此路径: enter image description hereComSpec C:\Windows\System32\cmd.exe


0

将maxBuffer增加解决了我的问题。

const child_process = require('child_process');

var command = 'git ls-files . --ignored --exclude-standard --others';

child_process.execSync(command, {
   maxBuffer: 1024 ** 6,
});

0
对我来说问题是我的解决方案目录在不同的驱动器上。在C盘上创建我的解决方案解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接