如何调试node.js上的“Error: spawn ENOENT”?

时间:2022-04-26 15:27:25

When I get the following error:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34)

What procedure can I follow to fix it?

Author note: Lots of issues with this error encouraged me to post this question for future references.

作者注:这个错误的很多问题鼓励我把这个问题发布到以后的参考文献中。

Related questions:

相关问题:

16 个解决方案

#1


163  

I found a particular easy way to get the idea of the root cause of:

我找到了一种特别简单的方法来理解:

Error: spawn ENOENT

The problem of this error is, there is really little information in the error message to tell you where the call site is, i.e. which executable/command is not found, especially when you have a large code base where there are a lot of spawn calls. On the other hand, if we know the exact command that cause the error then we can follow @laconbass' answer to fix the problem.

这个错误的问题是,错误消息中几乎没有什么信息可以告诉您调用站点在哪里,即没有找到哪个可执行/命令,特别是当您有一个很大的代码库,其中有很多派生调用时。另一方面,如果我们知道导致错误的确切命令,那么我们可以按照@laconbass的回答来修复问题。

I found a very easy way to spot which command cause the problem rather than adding event listeners everywhere in your code as suggested in @laconbass' answer. The key idea is to wrap the original spawn call with a wrapper which prints the arguments send to the spawn call.

我找到了一种非常简单的方法来确定哪个命令导致了问题,而不是像@laconbass的答案中建议的那样在代码中到处添加事件监听器。关键思想是用一个包装器包装原始的衍生调用,该包装器打印发送给衍生调用的参数。

Here is the wrapper function, put it at the top of the index.js or whatever your server's starting script.

这是包装器函数,将它放在索引的顶部。js或任何服务器的启动脚本。

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        console.log('spawn called');
        console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();

Then the next time you run your application, before the uncaught exception's message you will see something like that:

然后下次运行应用程序时,在未捕获异常的消息之前,您将看到如下内容:

spawn called
{ '0': 'hg',
  '1': [],
  '2':
   { cwd: '/* omitted */',
     env: { IP: '0.0.0.0' },
     args: [] } }

In this way you can easily know which command actually is executed and then you can find out why nodejs cannot find the executable to fix the problem.

通过这种方式,您可以很容易地知道实际执行了哪个命令,然后您就可以发现为什么nodejs无法找到可执行文件来解决这个问题。

#2


80  

Step 1: Ensure spawn is called the right way

First, review the docs for child_process.spawn( command, args, options ):

首先,查看child_process中的文档。衍生(命令,args,选项):

Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.

使用给定的命令启动一个新的进程,并在args中使用命令行参数。如果省略,args默认为空数组。

The third argument is used to specify additional options, which defaults to:

第三个参数用于指定附加选项,默认为:

{ cwd: undefined, env: process.env }

{cwd: undefined, env: process。env }

Use env to specify environment variables that will be visible to the new process, the default is process.env.

使用env指定新进程可见的环境变量,默认值是process.env。

Ensure you are not putting any command line arguments in command and the whole spawn call is valid. Proceed to next step.

确保您没有在命令中放入任何命令行参数,并且整个衍生调用是有效的。继续下一步。

Step 2: Identify the Event Emitter that emits the error event

Search on your source code for each call to spawn, or child_process.spawn, i.e.

在源代码中搜索要生成的每个调用,或child_process。产卵。

spawn('some-command', [ '--help' ]);

and attach there an event listener for the 'error' event, so you get noticed the exact Event Emitter that is throwing it as 'Unhandled'. After debugging, that handler can be removed.

并为“error”事件附加一个事件侦听器,这样您就会注意到将其作为“未处理”抛出的确切事件发射器。调试之后,可以删除该处理程序。

spawn('some-command', [ '--help' ])
  .on('error', function( err ){ throw err })
;

Execute and you should get the file path and line number where your 'error' listener was registered. Something like:

执行时,您应该获得注册了“error”侦听器的文件路径和行号。喜欢的东西:

/file/that/registers/the/error/listener.js:29
      throw err;
            ^
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34)

If the first two lines are still

如果前两行还是

events.js:72
        throw er; // Unhandled 'error' event

do this step again until they are not. You must identify the listener that emits the error before going on next step.

重复这个步骤,直到它们不是。在继续下一步之前,必须识别发出错误的侦听器。

Step 3: Ensure the environment variable $PATH is set

There are two possible scenarios:

有两种可能的情况:

  1. You rely on the default spawn behaviour, so child process environment will be the same as process.env.
  2. 您依赖于默认的衍生行为,因此子进程环境将与process.env相同。
  3. You are explicity passing an env object to spawn on the options argument.
  4. 您可以通过一个env对象来生成选项参数。

In both scenarios, you must inspect the PATH key on the environment object that the spawned child process will use.

在这两个场景中,您必须检查生成的子进程将使用的环境对象上的PATH键。

Example for scenario 1

示例场景1

// inspect the PATH key on process.env
console.log( process.env.PATH );
spawn('some-command', ['--help']);

Example for scenario 2

示例场景2

var env = getEnvKeyValuePairsSomeHow();
// inspect the PATH key on the env object
console.log( env.PATH );
spawn('some-command', ['--help'], { env: env });

The absence of PATH (i.e., it's undefined) will cause spawn to emit the ENOENT error, as it will not be possible to locate any command unless it's an absolute path to the executable file.

没有路径(即,它是未定义的)将导致spawn产生ENOENT错误,因为它不可能找到任何命令,除非它是到可执行文件的绝对路径。

When PATH is correctly set, proceed to next step. It should be a directory, or a list of directories. Last case is the usual.

正确设置路径后,继续下一步。它应该是一个目录,或者一个目录列表。最后一个例子是常见的。

Step 4: Ensure command exists on a directory of those defined in PATH

Spawn may emit the ENOENT error if the filename command (i.e, 'some-command') does not exist in at least one of the directories defined on PATH.

如果文件名命令(i),派生可能会产生ENOENT错误。e,“some-command”)在路径上定义的目录中至少有一个不存在。

Locate the exact place of command. On most linux distributions, this can be done from a terminal with the which command. It will tell you the absolute path to the executable file (like above), or tell if it's not found.

找到确切的指挥地点。在大多数linux发行版中,这可以通过使用which命令的终端完成。它将告诉您可执行文件的绝对路径(如上所示),或者告诉您是否找到它。

Example usage of which and its output when a command is found

当找到一个命令时,它的用法及其输出的示例

> which some-command
some-command is /usr/bin/some-command

Example usage of which and its output when a command is not found

当没有找到命令时,它的用法及其输出

> which some-command
bash: type: some-command: not found

miss-installed programs are the most common cause for a not found command. Refer to each command documentation if needed and install it.

错误安装的程序是导致未找到命令的最常见原因。如果需要,请参考每个命令文档并安装它。

When command is a simple script file ensure it's accessible from a directory on the PATH. If it's not, either move it to one or make a link to it.

当命令是一个简单的脚本文件时,确保可以从路径上的目录访问它。如果不是,要么把它移到一个,要么链接到它。

Once you determine PATH is correctly set and command is accessible from it, you should be able to spawn your child process without spawn ENOENT being thrown.

一旦您确定路径被正确设置并且命令可以从路径中访问,您就应该能够生成子进程,而不需要生成引发事件的指令。

#3


21  

As @DanielImfeld pointed it, ENOENT will be thrown if you specify "cwd" in the options, but the given directory does not exist.

正如@DanielImfeld指出的,如果在选项中指定“cwd”,ENOENT将被抛出,但是给定的目录不存在。

#4


19  

Windows solution: Replace spawn with node-cross-spawn. For instance like this at the beginning of your app.js:

Windows解决方案:用节点交叉衍生替换衍生。例如,在你的app.js的开头:

(function() {
    var childProcess = require("child_process");
    childProcess.spawn = require('cross-spawn');
})(); 

#5


14  

For anyone who might stumble upon this, if all the other answers do not help and you are on Windows, know that there is currently a big issue with spawn on Windows and the PATHEXT environment variable that can cause certain calls to spawn to not work depending on how the target command is installed.

对于那些可能偶然发现这个,如果所有其他答案不帮助你在Windows上,知道目前的一个大问题产生在Windows和列出环境变量,可能会导致某些电话产卵不取决于目标命令安装工作。

#6


12  

@laconbass's answer helped me and is probably most correct.

@laconbass的回答帮助了我,而且可能是最正确的。

I came here because I was using spawn incorrectly. As a simple example:

我来这里是因为我不正确地使用衍生。作为一个简单的例子:

this is incorrect:

这是不正确的:

const s = cp.spawn('npm install -D suman', [], {
    cwd: root
});

this is incorrect:

这是不正确的:

const s = cp.spawn('npm', ['install -D suman'], {
    cwd: root
});

this is correct:

这是正确的:

const s = cp.spawn('npm', ['install','-D','suman'], {
    cwd: root
});

#7


7  

For ENOENT on Windows, https://github.com/nodejs/node-v0.x-archive/issues/2318#issuecomment-249355505 fix it.

对于Windows上的ENOENT,请使用https://github.com/nodejs/node-v0.x-archive/issues/2318# issues ecomecomecomecomecom-249355505修复它。

e.g. replace spawn('npm', ['-v'], {stdio: 'inherit'}) with:

如更换产卵(npm,[' v '],{ stdio:“继承”}):

  • for all node.js version:

    对所有节点。js版本:

    spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['-v'], {stdio: 'inherit'})
    
  • for node.js 5.x and later:

    为节点。js 5。x和后:

    spawn('npm', ['-v'], {stdio: 'inherit', shell: true})
    

#8


6  

In my case, I was getting this error thrown due to the necessary dependent system resources not being installed.

在我的示例中,由于没有安装必要的依赖系统资源,导致抛出这个错误。

More specifically, I have a NodeJS app that is utilizing ImageMagick. Despite having the npm package installed, the core Linux ImageMagick was not installed. I did an apt-get to install ImageMagick and after that all worked great!

更具体地说,我有一个使用ImageMagick的NodeJS应用程序。尽管已经安装了npm包,但是内核Linux ImageMagick并没有安装。我做了一个apt-get来安装ImageMagick,之后一切都很好!

#9


1  

I ran into the same problem, but I found a simple way to fix it. It appears to be spawn() errors if the program has been added to the PATH by the user (e.g. normal system commands work).

我遇到了同样的问题,但我找到了一个简单的方法来解决它。如果程序被用户添加到路径中(例如,正常的系统命令可以工作),那么就会产生()错误。

To fix this, you can use the which module (npm install --save which):

要解决这个问题,可以使用哪个模块(npm安装——保存哪个模块):

// Require which and child_process
const which = require('which');
const spawn = require('child_process').spawn;
// Find npm in PATH
const npm = which.sync('npm');
// Execute
const noErrorSpawn = spawn(npm, ['install']);

#10


1  

Ensure module to be executed is installed or full path to command if it's not a node module

确保要执行的模块已安装,如果不是节点模块,则确保其为命令的完整路径

#11


1  

Use require('child_process').exec instead of spawn for a more specific error message!

使用要求(“child_process”)。执行,而不是衍生一个更具体的错误消息!

for example:

例如:

var exec = require('child_process').exec;
var commandStr = 'java -jar something.jar';

exec(commandStr, function(error, stdout, stderr) {
  if(error || stderr) console.log(error || stderr);
  else console.log(stdout);
});

#12


0  

I was getting this error when trying to debug a node.js program from within VS Code editor on a Debian Linux system. I noticed the same thing worked OK on Windows. The solutions previously given here weren't much help because I hadn't written any "spawn" commands. The offending code was presumably written by Microsoft and hidden under the hood of the VS Code program.

我在调试一个节点时遇到了这个错误。在Debian Linux系统中,js程序来自于VS代码编辑器。我注意到同样的东西在Windows上运行良好。前面给出的解决方案没有多大帮助,因为我没有编写任何“衍生”命令。违规的代码可能是微软编写的,隐藏在VS代码程序的外壳之下。

Next I noticed that node.js is called node on Windows but on Debian (and presumably on Debian-based systems such as Ubuntu) it's called nodejs. So I created an alias - from a root terminal, I ran

接下来我注意到了这个节点。js在Windows上被称为node,但在Debian(以及基于Debian的系统,比如Ubuntu)上被称为nodejs。所以我创建了一个别名——从根终端开始,我运行

ln -s /usr/bin/nodejs /usr/local/bin/node

ln - s /usr/bin/nodejs /usr/local/bin/node

and this solved the problem. The same or a similar procedure will presumably work in other cases where your node.js is called nodejs but you're running a program which expects it to be called node, or vice-versa.

这就解决了问题。在您的节点所在的其他情况下,相同或类似的过程可能也适用。js被称为nodejs,但您运行的程序期望它被称为node,反之亦然。

#13


0  

I got the same error for windows 8.The issue is because of an environment variable of your system path is missing . Add "C:\Windows\System32\" value to your system PATH variable.

windows 8也有同样的错误。问题是由于缺少系统路径的环境变量。将“C:\Windows\System32\”值添加到系统路径变量中。

#14


0  

If you're on Windows Node.js does some funny business when handling quotes that may result in you issuing a command that you know works from the console, but does not when run in Node. For example the following should work:

如果你在Windows节点上。js在处理引号时做了一些有趣的事情,这些引号可能导致您发出命令,您知道该命令在控制台工作,但在Node中运行时不会。例如:

spawn('ping', ['"8.8.8.8"'], {});

but fails. There's a fantastically undocumented option windowsVerbatimArguments for handling quotes/similar that seems to do the trick, just be sure to add the following to your opts object:

但失败。有一个奇妙的没有文档记载的选项windowswithatimarguments用于处理报价/类似的文件,这似乎可以达到目的,只要确保在您的opts对象中添加以下内容即可:

const opts = {
    windowsVerbatimArguments: true
};

and your command should be back in business.

你的命令应该会恢复正常。

 spawn('ping', ['"8.8.8.8"'], { windowsVerbatimArguments: true });

#15


0  

I was also going through this annoying problem while running my test cases, so I tried many ways to get across it. But the way works for me is to run your test runner from the directory which contains your main file which includes your nodejs spawn function something like this:

我在运行测试用例时也遇到了这个烦人的问题,所以我尝试了很多方法来解决它。但是对我来说,方法是在包含你的主文件的目录下运行你的测试运行器其中包含nodejs衍生函数如下:

nodeProcess = spawn('node',params, {cwd: '../../node/', detached: true });

For example, this file name is test.js, so just move to the folder which contains it. In my case, it is test folder like this:

例如,这个文件名是test。然后移动到包含它的文件夹。在我的例子中,它是这样的测试文件夹:

cd root/test/

then from run your test runner in my case its mocha so it will be like this:

从运行你的测试运行,在我的情况下,它的摩卡,将会是这样的:

mocha test.js

I have wasted my more than one day to figure it out. Enjoy!!

我浪费了一天多的时间才弄明白。享受! !

#16


-1  

Add C:\Windows\System32\ to the path environment variable.

C:\Windows\System32\添加到path环境变量。

Steps

  1. Go to my computer and properties

    去我的电脑和物业那里

  2. Click on Advanced settings

    点击高级设置

  3. Then on Environment variables

    然后在环境变量

  4. Select Path and then click on edit

    选择Path,然后单击edit

  5. Paste the following if not already present: C:\Windows\System32\

    粘贴以下如果不是已经存在:C:\Windows\System32\

  6. Close the command prompt

    关闭命令提示符

  7. Run the command that you wanted to run

    运行您想要运行的命令

如何调试node.js上的“Error: spawn ENOENT”?

#1


163  

I found a particular easy way to get the idea of the root cause of:

我找到了一种特别简单的方法来理解:

Error: spawn ENOENT

The problem of this error is, there is really little information in the error message to tell you where the call site is, i.e. which executable/command is not found, especially when you have a large code base where there are a lot of spawn calls. On the other hand, if we know the exact command that cause the error then we can follow @laconbass' answer to fix the problem.

这个错误的问题是,错误消息中几乎没有什么信息可以告诉您调用站点在哪里,即没有找到哪个可执行/命令,特别是当您有一个很大的代码库,其中有很多派生调用时。另一方面,如果我们知道导致错误的确切命令,那么我们可以按照@laconbass的回答来修复问题。

I found a very easy way to spot which command cause the problem rather than adding event listeners everywhere in your code as suggested in @laconbass' answer. The key idea is to wrap the original spawn call with a wrapper which prints the arguments send to the spawn call.

我找到了一种非常简单的方法来确定哪个命令导致了问题,而不是像@laconbass的答案中建议的那样在代码中到处添加事件监听器。关键思想是用一个包装器包装原始的衍生调用,该包装器打印发送给衍生调用的参数。

Here is the wrapper function, put it at the top of the index.js or whatever your server's starting script.

这是包装器函数,将它放在索引的顶部。js或任何服务器的启动脚本。

(function() {
    var childProcess = require("child_process");
    var oldSpawn = childProcess.spawn;
    function mySpawn() {
        console.log('spawn called');
        console.log(arguments);
        var result = oldSpawn.apply(this, arguments);
        return result;
    }
    childProcess.spawn = mySpawn;
})();

Then the next time you run your application, before the uncaught exception's message you will see something like that:

然后下次运行应用程序时,在未捕获异常的消息之前,您将看到如下内容:

spawn called
{ '0': 'hg',
  '1': [],
  '2':
   { cwd: '/* omitted */',
     env: { IP: '0.0.0.0' },
     args: [] } }

In this way you can easily know which command actually is executed and then you can find out why nodejs cannot find the executable to fix the problem.

通过这种方式,您可以很容易地知道实际执行了哪个命令,然后您就可以发现为什么nodejs无法找到可执行文件来解决这个问题。

#2


80  

Step 1: Ensure spawn is called the right way

First, review the docs for child_process.spawn( command, args, options ):

首先,查看child_process中的文档。衍生(命令,args,选项):

Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.

使用给定的命令启动一个新的进程,并在args中使用命令行参数。如果省略,args默认为空数组。

The third argument is used to specify additional options, which defaults to:

第三个参数用于指定附加选项,默认为:

{ cwd: undefined, env: process.env }

{cwd: undefined, env: process。env }

Use env to specify environment variables that will be visible to the new process, the default is process.env.

使用env指定新进程可见的环境变量,默认值是process.env。

Ensure you are not putting any command line arguments in command and the whole spawn call is valid. Proceed to next step.

确保您没有在命令中放入任何命令行参数,并且整个衍生调用是有效的。继续下一步。

Step 2: Identify the Event Emitter that emits the error event

Search on your source code for each call to spawn, or child_process.spawn, i.e.

在源代码中搜索要生成的每个调用,或child_process。产卵。

spawn('some-command', [ '--help' ]);

and attach there an event listener for the 'error' event, so you get noticed the exact Event Emitter that is throwing it as 'Unhandled'. After debugging, that handler can be removed.

并为“error”事件附加一个事件侦听器,这样您就会注意到将其作为“未处理”抛出的确切事件发射器。调试之后,可以删除该处理程序。

spawn('some-command', [ '--help' ])
  .on('error', function( err ){ throw err })
;

Execute and you should get the file path and line number where your 'error' listener was registered. Something like:

执行时,您应该获得注册了“error”侦听器的文件路径和行号。喜欢的东西:

/file/that/registers/the/error/listener.js:29
      throw err;
            ^
Error: spawn ENOENT
    at errnoException (child_process.js:1000:11)
    at Process.ChildProcess._handle.onexit (child_process.js:791:34)

If the first two lines are still

如果前两行还是

events.js:72
        throw er; // Unhandled 'error' event

do this step again until they are not. You must identify the listener that emits the error before going on next step.

重复这个步骤,直到它们不是。在继续下一步之前,必须识别发出错误的侦听器。

Step 3: Ensure the environment variable $PATH is set

There are two possible scenarios:

有两种可能的情况:

  1. You rely on the default spawn behaviour, so child process environment will be the same as process.env.
  2. 您依赖于默认的衍生行为,因此子进程环境将与process.env相同。
  3. You are explicity passing an env object to spawn on the options argument.
  4. 您可以通过一个env对象来生成选项参数。

In both scenarios, you must inspect the PATH key on the environment object that the spawned child process will use.

在这两个场景中,您必须检查生成的子进程将使用的环境对象上的PATH键。

Example for scenario 1

示例场景1

// inspect the PATH key on process.env
console.log( process.env.PATH );
spawn('some-command', ['--help']);

Example for scenario 2

示例场景2

var env = getEnvKeyValuePairsSomeHow();
// inspect the PATH key on the env object
console.log( env.PATH );
spawn('some-command', ['--help'], { env: env });

The absence of PATH (i.e., it's undefined) will cause spawn to emit the ENOENT error, as it will not be possible to locate any command unless it's an absolute path to the executable file.

没有路径(即,它是未定义的)将导致spawn产生ENOENT错误,因为它不可能找到任何命令,除非它是到可执行文件的绝对路径。

When PATH is correctly set, proceed to next step. It should be a directory, or a list of directories. Last case is the usual.

正确设置路径后,继续下一步。它应该是一个目录,或者一个目录列表。最后一个例子是常见的。

Step 4: Ensure command exists on a directory of those defined in PATH

Spawn may emit the ENOENT error if the filename command (i.e, 'some-command') does not exist in at least one of the directories defined on PATH.

如果文件名命令(i),派生可能会产生ENOENT错误。e,“some-command”)在路径上定义的目录中至少有一个不存在。

Locate the exact place of command. On most linux distributions, this can be done from a terminal with the which command. It will tell you the absolute path to the executable file (like above), or tell if it's not found.

找到确切的指挥地点。在大多数linux发行版中,这可以通过使用which命令的终端完成。它将告诉您可执行文件的绝对路径(如上所示),或者告诉您是否找到它。

Example usage of which and its output when a command is found

当找到一个命令时,它的用法及其输出的示例

> which some-command
some-command is /usr/bin/some-command

Example usage of which and its output when a command is not found

当没有找到命令时,它的用法及其输出

> which some-command
bash: type: some-command: not found

miss-installed programs are the most common cause for a not found command. Refer to each command documentation if needed and install it.

错误安装的程序是导致未找到命令的最常见原因。如果需要,请参考每个命令文档并安装它。

When command is a simple script file ensure it's accessible from a directory on the PATH. If it's not, either move it to one or make a link to it.

当命令是一个简单的脚本文件时,确保可以从路径上的目录访问它。如果不是,要么把它移到一个,要么链接到它。

Once you determine PATH is correctly set and command is accessible from it, you should be able to spawn your child process without spawn ENOENT being thrown.

一旦您确定路径被正确设置并且命令可以从路径中访问,您就应该能够生成子进程,而不需要生成引发事件的指令。

#3


21  

As @DanielImfeld pointed it, ENOENT will be thrown if you specify "cwd" in the options, but the given directory does not exist.

正如@DanielImfeld指出的,如果在选项中指定“cwd”,ENOENT将被抛出,但是给定的目录不存在。

#4


19  

Windows solution: Replace spawn with node-cross-spawn. For instance like this at the beginning of your app.js:

Windows解决方案:用节点交叉衍生替换衍生。例如,在你的app.js的开头:

(function() {
    var childProcess = require("child_process");
    childProcess.spawn = require('cross-spawn');
})(); 

#5


14  

For anyone who might stumble upon this, if all the other answers do not help and you are on Windows, know that there is currently a big issue with spawn on Windows and the PATHEXT environment variable that can cause certain calls to spawn to not work depending on how the target command is installed.

对于那些可能偶然发现这个,如果所有其他答案不帮助你在Windows上,知道目前的一个大问题产生在Windows和列出环境变量,可能会导致某些电话产卵不取决于目标命令安装工作。

#6


12  

@laconbass's answer helped me and is probably most correct.

@laconbass的回答帮助了我,而且可能是最正确的。

I came here because I was using spawn incorrectly. As a simple example:

我来这里是因为我不正确地使用衍生。作为一个简单的例子:

this is incorrect:

这是不正确的:

const s = cp.spawn('npm install -D suman', [], {
    cwd: root
});

this is incorrect:

这是不正确的:

const s = cp.spawn('npm', ['install -D suman'], {
    cwd: root
});

this is correct:

这是正确的:

const s = cp.spawn('npm', ['install','-D','suman'], {
    cwd: root
});

#7


7  

For ENOENT on Windows, https://github.com/nodejs/node-v0.x-archive/issues/2318#issuecomment-249355505 fix it.

对于Windows上的ENOENT,请使用https://github.com/nodejs/node-v0.x-archive/issues/2318# issues ecomecomecomecomecom-249355505修复它。

e.g. replace spawn('npm', ['-v'], {stdio: 'inherit'}) with:

如更换产卵(npm,[' v '],{ stdio:“继承”}):

  • for all node.js version:

    对所有节点。js版本:

    spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['-v'], {stdio: 'inherit'})
    
  • for node.js 5.x and later:

    为节点。js 5。x和后:

    spawn('npm', ['-v'], {stdio: 'inherit', shell: true})
    

#8


6  

In my case, I was getting this error thrown due to the necessary dependent system resources not being installed.

在我的示例中,由于没有安装必要的依赖系统资源,导致抛出这个错误。

More specifically, I have a NodeJS app that is utilizing ImageMagick. Despite having the npm package installed, the core Linux ImageMagick was not installed. I did an apt-get to install ImageMagick and after that all worked great!

更具体地说,我有一个使用ImageMagick的NodeJS应用程序。尽管已经安装了npm包,但是内核Linux ImageMagick并没有安装。我做了一个apt-get来安装ImageMagick,之后一切都很好!

#9


1  

I ran into the same problem, but I found a simple way to fix it. It appears to be spawn() errors if the program has been added to the PATH by the user (e.g. normal system commands work).

我遇到了同样的问题,但我找到了一个简单的方法来解决它。如果程序被用户添加到路径中(例如,正常的系统命令可以工作),那么就会产生()错误。

To fix this, you can use the which module (npm install --save which):

要解决这个问题,可以使用哪个模块(npm安装——保存哪个模块):

// Require which and child_process
const which = require('which');
const spawn = require('child_process').spawn;
// Find npm in PATH
const npm = which.sync('npm');
// Execute
const noErrorSpawn = spawn(npm, ['install']);

#10


1  

Ensure module to be executed is installed or full path to command if it's not a node module

确保要执行的模块已安装,如果不是节点模块,则确保其为命令的完整路径

#11


1  

Use require('child_process').exec instead of spawn for a more specific error message!

使用要求(“child_process”)。执行,而不是衍生一个更具体的错误消息!

for example:

例如:

var exec = require('child_process').exec;
var commandStr = 'java -jar something.jar';

exec(commandStr, function(error, stdout, stderr) {
  if(error || stderr) console.log(error || stderr);
  else console.log(stdout);
});

#12


0  

I was getting this error when trying to debug a node.js program from within VS Code editor on a Debian Linux system. I noticed the same thing worked OK on Windows. The solutions previously given here weren't much help because I hadn't written any "spawn" commands. The offending code was presumably written by Microsoft and hidden under the hood of the VS Code program.

我在调试一个节点时遇到了这个错误。在Debian Linux系统中,js程序来自于VS代码编辑器。我注意到同样的东西在Windows上运行良好。前面给出的解决方案没有多大帮助,因为我没有编写任何“衍生”命令。违规的代码可能是微软编写的,隐藏在VS代码程序的外壳之下。

Next I noticed that node.js is called node on Windows but on Debian (and presumably on Debian-based systems such as Ubuntu) it's called nodejs. So I created an alias - from a root terminal, I ran

接下来我注意到了这个节点。js在Windows上被称为node,但在Debian(以及基于Debian的系统,比如Ubuntu)上被称为nodejs。所以我创建了一个别名——从根终端开始,我运行

ln -s /usr/bin/nodejs /usr/local/bin/node

ln - s /usr/bin/nodejs /usr/local/bin/node

and this solved the problem. The same or a similar procedure will presumably work in other cases where your node.js is called nodejs but you're running a program which expects it to be called node, or vice-versa.

这就解决了问题。在您的节点所在的其他情况下,相同或类似的过程可能也适用。js被称为nodejs,但您运行的程序期望它被称为node,反之亦然。

#13


0  

I got the same error for windows 8.The issue is because of an environment variable of your system path is missing . Add "C:\Windows\System32\" value to your system PATH variable.

windows 8也有同样的错误。问题是由于缺少系统路径的环境变量。将“C:\Windows\System32\”值添加到系统路径变量中。

#14


0  

If you're on Windows Node.js does some funny business when handling quotes that may result in you issuing a command that you know works from the console, but does not when run in Node. For example the following should work:

如果你在Windows节点上。js在处理引号时做了一些有趣的事情,这些引号可能导致您发出命令,您知道该命令在控制台工作,但在Node中运行时不会。例如:

spawn('ping', ['"8.8.8.8"'], {});

but fails. There's a fantastically undocumented option windowsVerbatimArguments for handling quotes/similar that seems to do the trick, just be sure to add the following to your opts object:

但失败。有一个奇妙的没有文档记载的选项windowswithatimarguments用于处理报价/类似的文件,这似乎可以达到目的,只要确保在您的opts对象中添加以下内容即可:

const opts = {
    windowsVerbatimArguments: true
};

and your command should be back in business.

你的命令应该会恢复正常。

 spawn('ping', ['"8.8.8.8"'], { windowsVerbatimArguments: true });

#15


0  

I was also going through this annoying problem while running my test cases, so I tried many ways to get across it. But the way works for me is to run your test runner from the directory which contains your main file which includes your nodejs spawn function something like this:

我在运行测试用例时也遇到了这个烦人的问题,所以我尝试了很多方法来解决它。但是对我来说,方法是在包含你的主文件的目录下运行你的测试运行器其中包含nodejs衍生函数如下:

nodeProcess = spawn('node',params, {cwd: '../../node/', detached: true });

For example, this file name is test.js, so just move to the folder which contains it. In my case, it is test folder like this:

例如,这个文件名是test。然后移动到包含它的文件夹。在我的例子中,它是这样的测试文件夹:

cd root/test/

then from run your test runner in my case its mocha so it will be like this:

从运行你的测试运行,在我的情况下,它的摩卡,将会是这样的:

mocha test.js

I have wasted my more than one day to figure it out. Enjoy!!

我浪费了一天多的时间才弄明白。享受! !

#16


-1  

Add C:\Windows\System32\ to the path environment variable.

C:\Windows\System32\添加到path环境变量。

Steps

  1. Go to my computer and properties

    去我的电脑和物业那里

  2. Click on Advanced settings

    点击高级设置

  3. Then on Environment variables

    然后在环境变量

  4. Select Path and then click on edit

    选择Path,然后单击edit

  5. Paste the following if not already present: C:\Windows\System32\

    粘贴以下如果不是已经存在:C:\Windows\System32\

  6. Close the command prompt

    关闭命令提示符

  7. Run the command that you wanted to run

    运行您想要运行的命令

如何调试node.js上的“Error: spawn ENOENT”?