0

I'm new to Angular, when I try running gulp in the angular folder, I get the following error:

[08:24:14] Using gulpfile .../angular/gulpfile.js
[08:24:14] Starting 'styles'...
[08:24:14] Starting 'angular'...
[08:24:14] Finished 'angular' after 6.44 ms
[08:24:14] Starting 'server'...
[08:24:14] Finished 'server' after 19 ms
[08:24:14] Starting 'watch'...
[08:24:14] Finished 'watch' after 53 ms
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: spawn ng build --watch --output-path ../../public/angular/dist ENOENT
    at exports._errnoException (util.js:870:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at doNTCallback2 (node.js:441:9)
    at process._tickCallback (node.js:355:17)
    at Function.Module.runMain (module.js:469:11)
    at startup (node.js:136:18)
    at node.js:963:3

I have already followed a similar post and tried:

rm -rf node_modules && npm cache clean && npm install

But, still get the same error.

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
Abhi
  • 4,123
  • 6
  • 45
  • 77

2 Answers2

0

Given your error message, it looks like your gulp task is calling:

spawn("ng build --watch --output-path ../../public/angular/dist")

Spawn requires arguments to be passed separately from the initial command, so you should use

spawn("ng", ["build", "--watch", "--output-path", "../../public/angular/dist"])

instead.

https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

aleroy
  • 981
  • 9
  • 11
0

I have faced the similar problem with NPM angular script on windows, there are couple of thing you can check.

Step 1. Add below code to the top, so that you know which command has problem

  /** add below childProcess logic to troubleshoot what's wrong if error happens. */
  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;
  spawn = childProcess.spawn;

Step 2. If you run your application on windows, you may need to use ng.cmd; otherwise, for other os, please use ng.sh

  const isWindows = /^win/.test(process.platform);
  const commandFileExtension = isWindows ? 'cmd' : 'sh';
  let child = spawn(`ng.${commandFileExtension}`, ['build']);
Robin Ding
  • 741
  • 6
  • 9