annieg4123
annieg4123

Reputation: 69

Angular Cli - ng command not found in terminal

Specs:

I'm new to programming (and stackoverflow). I installed Angular using the following command in the terminal: npm install -g @angular/cli. I ran into an EACCESS error but this resolved it. Initially I was able to run the ng command. But after closing and reopening the terminal, the "ng: command not found" error happened. I removed Angular and reinstalled it. But that didn't help.

Screenshot of the "ng: command does not exist" error

Screenshot of npm list -g --depth=0

Errors in uninstalling Angular part 1

Errors in uninstalling Angular part 2

echo $PATH

Upvotes: 2

Views: 16282

Answers (6)

Shreya
Shreya

Reputation: 79

Follow these steps:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

export NVM_DIR="/Users/Enter-Your-own-username/.nvm"

The below command will load nvm

[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

Type the below commands:

nvm install stable

nvm install node

Check for the installed version of the node to verify the installation node version

If you have node installed already then go for the below step:

npm install -g @angular/cli

And in the same terminal session type the below command:

ng version

    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/

Create a new project with:

ng new angularProject

ng server angularProject

type:

localhost:4200

That should do the job.

Upvotes: 0

andreas
andreas

Reputation: 256

Have you tried to check your NODE_PATH environment variable value ?

echo $NODE_PATH

If it's empty (and given your default location of installation in MacOS) try to set it into:

export NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node

To make it more permanent put it inside your ~/.bash_profile, then restart your terminal (otherwise source ~/.bash_profile)

Upvotes: 1

Orlov Andrey
Orlov Andrey

Reputation: 272

You need to install @angular/cli globally NOT locally. if you type:

npm install -g @angular/cli

in terminal and you will get the error: ng: command not found or different error, it's because you do not have access to the global directory of npm

The solution is: just use sudo

sudo npm install -g @angular/cli

Upvotes: 3

Jonathan Fuerth
Jonathan Fuerth

Reputation: 2190

If you're using an installation of Node and NPM based on Homebrew, your installation may not keep your globally installed packages in a predictable location (mine didn't).

These steps helped me ensure Angular CLI and other globally-installed packages land in a predictable location and now they work in my terminal.

Summary:

  1. Delete any existing NPM packages:

    sudo rm -rf /usr/local/lib/node_modules

  2. Delete your current installation of Node and NPM:

    brew uninstall node

  3. Preconfigure NPM to install packages in a location under your home directory:

    echo prefix=~/.npm-packages >> ~/.npmrc

  4. Add the new NPM packages location to your shell's search path:

    echo 'export PATH="$HOME/.npm-packages/bin:$PATH"' >> ~/.bash_profile

  5. Quit and re-launch the Terminal app

  6. Install Angular CLI in your fresh Node setup:

    npm install -g @angular/cli

Now the ng command should exist at ~/.npm-packages/bin/ng and you should be able to run ng --version successfully.

Upvotes: 0

Iancovici
Iancovici

Reputation: 5731

Nodejs website installation package installed it almost like it expected a pc. So we're seeing two issues here

  • Premission on directory it was installed, which can be fixed with chmod
  • Location unknown, which can be fixed with export

You can export by editing ~/.bash_profile and adding

export PATH="[Enter your path here]:$PATH"

But if you're new to this, I highly recommend to resort to helping installing software like homebrew approach very friendly mac installing/update for terminal.

Will want to uninstall your current node if possible.

Then it's

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

brew doctor

export PATH="/usr/local/bin:$PATH"

brew install node

npm install -g @angular/cli@latest 

Upvotes: 1

Paul Isaris
Paul Isaris

Reputation: 483

I would suggest you tried another way of installing nodejs, like using Node Version Manager. It is really easy to use and lets you run multiple version of nodejs and npm dependencies on the same machine.

As it is stated in npm documentation, EACCESS errors usually go away with the following strategies:

  1. Use a Version manager (like Node Version Manager)
  2. Change nodejs default installation directory

Upvotes: 1

Related Questions