How does Angular CLI runs behind?

Issue

I am working on a school project and I had an issue with my Angular "ng" command because it was not recognized by my system.

'ng' is not recognized as an internal or external command, operable program or batch file. 

However, my friend told me to run the command this way and it works magically.

C:/Users/[userName]/AppData/Roaming/npm/ng --version

I know the above path is the place where npm is installed, but I am still curious about how it works.

Which file in the npm folder runs the ng command? How come the simple ‘ng’ command does not work, but the full path command like above works? Which skills do I need to study to overcome issues like this(I would not be able to figure this out without my friend’s help)?

Solution

Most likely it doesn’t runs because you don’t have Angular CLI installed, or as a second guess, you don’t have your node install in the path variable. As a test, try running node -v to check if that’s the case.

The real "magic" behind the bare ng is that, when you globally install Angular CLI, it creates a ng.bat file in the main node folder, which is normally in the path. That bat file is just a loader for running the real Angular CLI, which is just a bunch of Javascript files like any node program (installed in the global "node_modules" folder).

In short, the path variable together with a conveniently placed ng.bat file plus the Windows command line convention of allowing omiting the file extension for bat files is what allows the trick of just using ng .......

Having to type the full path is an indicator that the PATH variable is missing (or set incorrectly).

The bundled npm follows the exactly same trick.

Answered By – Alejandro

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.