错误:生成npm ENOENT

时间:2017-04-05 11:49:26

标签: javascript node.js windows

我有一个JS应用程序。它在linux上运行良好但在Windows 10中我收到错误。

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

Error: spawn npm ENOENT
    at exports._errnoException (util.js:1028:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:193:32)
    at onErrorNT (internal/child_process.js:359:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:607:11)
    at run (bootstrap_node.js:422:7)
    at startup (bootstrap_node.js:143:9)
    at bootstrap_node.js:537:3

和不正确的代码是

const spawn = require('child_process').spawn;

const watching = [
  // {service: "babel-watch"},
  {service: "webpack-watch"},
  // {service: "sass-watch"},
  {service: "server-watch"}
];

watching.forEach(({service}) => {
  const child = spawn('npm', ['run', service]);
  child.stdout.on('data', d => console.log(d.toString()));
  child.stderr.on('data', d => console.log(d.toString()));
});

我在github中找到了这个错误的原因我想问题是spawn nodejs spawn Doc,它在Windows中无法正常工作。但我不知道如何修改这段代码以使其工作。有人能帮助我吗?

4 个答案:

答案 0 :(得分:42)

刚刚改变了这一行

const child = spawn('npm', ['run', service]);

到这一行

  const child = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run',  service]);

如果它运行npm.cmd,如果它的linux只是npm那么检查操作系统

答案 1 :(得分:2)

我知道有一个正确的答案,这个问题已经存在很长时间了,我的解决方案是基于@Armen Sanoyan 和 How do I determine the current operating system with Node.js

对我来说,@Armen Sanoyan 的回答不起作用,但有很大帮助。我改变了这条线并工作。

const child = (process.platform === 'win32' ? 'npm.cmd' : 'npm') + ' run ' + service;

希望能帮到你。

答案 2 :(得分:1)

我知道这看起来很明显,但请确保您没有将多个标志/选项放入一个字符串中:

// Auxiliary macro, does the heavy lifting
macro_rules! and_inner {
    // Finishing rule, assembles the actual output
    ( @ $var:ident { $( $finished:tt )* } from { $(,)? } ) => {
        {
            let mut $var = 0x01;
            
            $( $finished )*
            
            $var & 0x01
        }
    };
    
    // Parse negated case
    ( @ $var:ident {
            $( $finished:tt )*
        } from {
            ! $val:ident . $bit:literal , // only this line is processed here
            $( $rest_input:tt )*
        }
    ) => {
        and_inner!(@ $var {
            $( $finished )*
            $var &= !($val >> $bit);
        } from {
            $( $rest_input )*
        })
    };
    
    // Parse positive case
    ( @ $var:ident {
            $( $finished:tt )*
        } from {
            $val:ident . $bit:literal , // only this line is processed here
            $( $rest_input:tt )*
        }
    ) => {
        and_inner!(@ $var {
            $( $finished )*
            $var &= ($val >> $bit);
        } from {
            $( $rest_input )*
        })
    };
}
// Main macro
macro_rules! AND {
    // Entry rule prepares input for internal macro
    ( $( $input:tt )* ) => {
        and_inner!(@ tmp_var { } from { $($input)* , })
    };
}

答案 3 :(得分:0)

我遇到了同样的问题。我的应用程序代码在MAC中运行正常,但在Windows中却给出了与spawn命令相关的代码错误

使用command prompt

运行时发生错误

当我使用GIT bash启动应用程序时,没有发生错误。我不需要更改任何代码

相关问题