如何修复错误:在Windows 10中生成npm ENOENT?

时间:2019-05-29 18:19:25

标签: javascript node.js child-process

我正在尝试创建一个CLI,该CLI自动克隆我创建的vuex-store-starter模板。在安装时,它显示基于错误的错误:spawn npm ENOENT

任何帮助都将受到欢迎。

#! /usr/bin/env node

const {spawn} = require('child_process');



const name = process.argv[2];
if (!name || name.match(/[<>:"\/\\|?*\x00-\x1F]/)) {
  return console.log(`
  Invalid directory name.
  Usage: <vuex-store-starter-cli> <repo-name>  
`);
}

const URL = 'https://github.com/ChrisMichaelPerezSantiago/vuex-store-starter.git';

f('git', ['clone', URL, name])
  .then(() => {
    return f('rm', ['-rf', `${name}/.git`]);
  }).then(() => {
    console.log('Installing dependencies...');
    return f('npm', ['install'], {
      cwd: process.cwd() + '/' + name
    });
  }).then(() => {
    console.log('Done! ');
    console.log('');
    console.log('cd', name);
    console.log('npm run start');
    console.log('For more information check the package.json')
  });

function f(command, args, options = undefined) {
  const spawned = spawn(command, args, options);
  return new Promise((resolve) => {
    spawned.stdout.on('data', (data) => {
      console.log(data.toString());
    });
    spawned.stderr.on('data', (data) => {
      console.error(data.toString());
    });
    spawned.on('close', () => {
      resolve();
    });
  });
}

我遇到的错误输出

Cloning into 'vuex-store-starter-cli'...

Installing dependencies...
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn npm ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)

1 个答案:

答案 0 :(得分:0)

我通过添加以下内容纠正了该问题

const npm = which.sync('npm');

并更改以下内容

'npm', ['install']npm, ['install']