1

I'm trying to create a CLI that automatically clones the vuex-store-starter template that I created. At the time of installation, it shows an error based on Error: spawn npm ENOENT

Any help will be more than welcome.

#! /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();
    });
  });
}

The error output that I am having

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)
Chris Michael
  • 1,557
  • 3
  • 15
  • 23
  • Related https://stackoverflow.com/questions/27688804/how-do-i-debug-error-spawn-enoent-on-node-js – Alberto Rivera May 29 '19 at 18:47
  • 2
    I corrected the problem by adding the following: const npm = which.sync ('npm'); And changing the following 'npm', ['install'] by npm, ['install'], – Chris Michael May 29 '19 at 19:05

1 Answers1

1

I corrected the problem by adding the following

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

And changing the following

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

Chris Michael
  • 1,557
  • 3
  • 15
  • 23