ng: command not found while creating new project u

2020-01-27 16:12发布

问题:

Installed angular-cli globally using (npm install -g angular-cli) but when I'm trying to create project using ng new my-project it is throwing error:

ng: command not found

回答1:

The issue is simple, npm doesn't know about ng

Just run npm link @angular/cli and it should work seamlessly.



回答2:

First, angular-cli is deprecated and has been replaced with @angular/cli. So if you uninstall your existing angular-cli with npm uninstall angular-cli, then reinstall the package with the new name @angular/cli you might get some conflicts. My story on Windows 7 is:

I had installed angular-cli and reinstalled using npm install -g @angular/cli, but after doing some config changes to command-line tools, I started getting the ng command not found issue. I spent several hours trying to fix this but none of the above issues alone worked. I was able to fix it using these steps:

Install Rapid Environment Editor and remove any PATH entries for node, npm, angular-cli or @angular/cli. Node.js will be in your System path, npm and angular entries are in the User path.

Uninstall node.js and reinstall the current version (for me 6.11.1). Run Rapid Environment Editor again and make sure node.js and npm are in your System or User path. Uninstall any existing ng versions with:

npm uninstall -g angular-cli

npm uninstall -g @angular/cli

npm cache clean

Delete the C:\Users\%YOU%\AppData\Roaming\npm\node_modules\@angular folder.

Reboot, then, finally, run:

npm install -g @angular/cli

Then hold your breath and run:

ng -v

If you're lucky, you'll get some love. Hold your breath henceforward every time you run the ng command, because 'command not found' has magically reappeared for me several times after ng was running fine and I thought the problem was solved.



回答3:

Make sure that the npm directory is in your "Path" variable.

If the module is installed properly, it may work if you start it out of your global node module directory, but your command line tool doesn't know where to find the ng command when you are not in this directory.

For Win system variable add something like:

%USERPROFILE%\AppData\Roaming\npm

And if you use a unix-like terminal (emulator):

PATH=$PATH:[path_to_your_user_profile]/path-to-npm


回答4:

the easiest solution is (If you have already installed angular) :

1 remove the ng alias if existing

unalias ng

2 add the correct alias

alias ng="/Users/<user_name>/.npm-global/bin/ng"

3 run ng serve for example and it will work.



回答5:

For Mac run

npm install -g @angular/cli@latest

then run

alias ng="/usr/local/lib/node_modules/node/lib/node_modules/@angular/cli/bin/ng"

Check if working

ng version


回答6:

This is how I made it worked for me :).

1 - npm link @angular/cli

It will return you the path of cli, which will look like this

/usr/local/Cellar/node/11.3.0_1/lib/node_modules/@angular/cli

For this part, /11.3.0_1 please replace this with your respective node version that can be found by typing node --version

2 - cd ~/

3 - open .bash_profile

In the bash profile create an alias for cli like this,

alias ng="/usr/local/Cellar/node/11.3.0_1/lib/node_modules/@angular/cli/bin/ng"

4 - source ~/.bash_profile

This is how your .bash_profile will look like once you add alias to it.

Now typing ng in the terminal will display output shown in attached snapshot.

I hope this answer will be helpful.



回答7:

Run below commands:

npm uninstall -g angular-cli

npm uninstall -g @angular/cli

npm cache clean

npm install -g @angular/cli@latest

alias ng="C:/Users/itaas/.npm-global/ng" ( Location of ng file in npm folder)

And finally run :

ng -v 



回答8:

I've solved the same issue with adding an alias like:

alias ng="path-to-your-global-node-modules/angular-cli/bin/ng"


回答9:

In my case install angular cli

npm install -g @angular/cli@latest

update Nodejs to latest, then all should work fine.

...and if you still have that problem, it maybe because you run the command in shell and not in cmd (you need to run command in cmd), check this out and maybe it helps...



回答10:

First of all, check if your npm and node installed properly
with commands npm version and node -v.

If they are proper:

  1. Find the root global Directory of NPM npm root -g (it will give you root of your global npm store)

  2. Uninstall old angular cli with npm uninstall -g angular-cli and npm cache clean

  3. Reinstall new Version of angular npm install -g @angular/cli@latest

  4. make an Alias of Name ng:

alias ng="C:/ProgramData/npm/node_modules/@angular/cli/bin/ng"

alias ng="<ath-to-your-global-node-modules>/<angular cli path till ng>"
(from answered Oct 20 '16 at 15:30 @m.zemlyanoi )

then to check you can type ng -v



回答11:

Adding %AppData%\npm to the Win path have worked for me.

Source : https://github.com/angular/angular-cli/issues/1183 , the first comment.



回答12:

Do you install the node js package? https://nodejs.org/en/ Regards



回答13:

if you find this error when you are installing angular-cli, -bash: ng: command not found try this it works,

After removing Node from your system

install NVM from here https://github.com/creationix/nvm
Install Node via NVM: nvm install stable
run npm install -g angular-cli


回答14:

Make sure angular-cli is installed before trying to create a project. Windows users can install angular-cli without giving permission to command but MAC users have to use sudo before executing the command as follow:

sudo npm install -g angular-cli

Type a password when asked and press enter to proceed.



回答15:

Same problem here running Windows 10 x64 / NodeJS 6.9.1 / npm 3.10.9.
After installation of Angular CLI via npm:

'ng' command cannot be found

Do the following:

  1. Uninstalled npm with npm uninstall -g npm
  2. Uninstalled NodeJS via control panel / programs and features
  3. Downloaded and ran 'old' NodeJS installer release 6.5.0 from https://nodejs.org/download/release/v6.5.0/ (node-v6.5.0-x64.msi).
  4. After NodeJS 6.5.0 installation completed, open powershell and npm install -g angular-cli
  5. Grab quick coffee
  6. After installation completed, ng worked OK.

HTH



回答16:

According to npm, the angular-cli has been renamed to @angular/cli you can use the following syntax to install it.

npm install -g @angular/cli


回答17:

For Mac users:

1) Install @angular/cli globally

$ sudo npm install -g @angular/cli
/usr/local/Cellar/node/10.0.0/bin/ng -> /usr/local/Cellar/node/10.0.0/lib/node_modules/@angular/cli/bin/ng

2) Create alias for ng

alias ng="/usr/local/Cellar/node/10.0.0/lib/node_modules/@angular/cli/bin/ng"

Done



回答18:

try this :

alias ng="~/node_modules/@angular/cli/bin/ng"



回答19:

soluton for windows operating system only....... first step:

install nodejs version: nodev 8.1.2

second step: set up environment variable like: C:\ProgramFiles\nodejs

Third step: install angular use this command: npm install -g @angular/cli

after installation whereever you have to create project like: ng new first-project......



回答20:

For me (on MacOSX) I had to do:

nvm install stable
npm install -g angular-cli

This installed ng into:

/usr/local/lib/node_modules/@angular/cli/bin/ng

But npm did not put a link to ng into

/usr/local/bin/

Which was why it was not part of the %PATH and therefore available from the command line except via an absolute address.

So I used the following the create a link to ng:

sudo ln -sf /usr/local/lib/node_modules/\@angular/cli/bin/ng /usr/local/bin/ng


回答21:

If you are working in windows 7 and you can not run command start with ng

please, update the angular/CLI at once and try to use ng commands

use below comman to update latest CLI

npm install -g @angular/cli@latest


回答22:

This works to update your angular/cli //*Global package (cmd as administrator)

npm uninstall -g @angular/cli
npm cache verify
npm install -g @angular/cli@latest


回答23:

Firstly install '@angular/cli' by using npm as global module

sudo npm install -g @angular/cli

After that try to ping the cli by using 'ng' command from the terminal window.If any problem like

Command 'ng' not found

Then you need to manually setup the

ng

command availability.You set this step by running the following command.

ln -s path-to-your-ng-command /bin

eg:

sudo ln -s /opt/node/lib/node_modules/@angular/cli/bin/ng /bin/



回答24:

In my case, it was happening when I restarted working on an adjacent tab. It was the npm which was not set. Please check the version of npm wither using

npm -v

or if you are having nvm then

nvm list

or

nvm current

I set it to 8.9.1 and then it started working again. I hope it helps.



回答25:

This worked for me:

  1. Go to C:\Users{{users}}\AppData\Roaming and delete the npm folder
  2. Uninstall node and install it again
  3. Run the command to install angular cli

    npm install -g @angular/cli
    


回答26:

In my case, I was simply running the wrong node version.

I had just previously installed a new node version to play around with Angular (2). At work we use 6.x so that is my default in nvm. After restarting the laptop ng stopped working simply because I was running node 6.x again. So for me it was simply a matter of using the version with which I was installing the Angular CLI:

nvm use node // with the node alias pointing to the right version

or

nvm use v8.11.3 // if you happen to know the version

Check your installed versions and aliases with

nvm list 


回答27:

If you have a MacOS computer (mine is MOJAVE 10.14.2), just add these lines to the end of your ~/.bash_profile file:

export ANGULAR=~/.nvm/versions/node/v10.8.0/bin/ng
export PATH=$ANGULAR:$PATH

Notice that v10.8.0 is the version of my installed Node.js. To get which version is yours, run this:

node --version

When done, reload it via your terminal/bash:

cd ~
source .bash_profile

After doing these steps you should be able to run your ng binary file.



回答28:

running

export PATH=$PATH:/c/Users/myusername/AppData/Roaming/npm 

helped.

Make sure your actual username is in the myusername section



回答29:

Repairing NodeJS installation on windows resolved it for me.



回答30:

If you have zsh installed add alias to .zshrc file in home directory as well.