1

Specs:

  • MacOS 10.12.6

  • Node.js v8.9.4

  • NPM 5.6.0

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

annieg4123
  • 59
  • 1
  • 3
  • 10
  • 1
    What does typing `npm list -g --depth=0` say – Iancovici Jan 06 '18 at 19:04
  • I tried that before and it says that @angular/cli@1.6.3 is in the following path: /Users//.npm-global/lib – annieg4123 Jan 06 '18 at 19:16
  • 1
    that doesn't sound like a mac. – Iancovici Jan 06 '18 at 19:19
  • Are you running a virtual machine? – Iancovici Jan 06 '18 at 19:21
  • I'm using a mac and using the terminal built into my mac. so I'm not sure what you mean. – annieg4123 Jan 06 '18 at 19:23
  • Open a terminal in your mac, run `npm list -g --depth=0` in your mac terminal and share a screenshot or copy paste output. In mac you usually see /usr, /Users is more common in windows. – Iancovici Jan 06 '18 at 19:24
  • Ok, I added the screenshot in the description. – annieg4123 Jan 06 '18 at 19:27
  • 1
    Try following 3 things,1. `npm uninstall -g @angular/cli` 2. `npm cache verify` 3. `npm install -g @angular/cli@latest` (uninstall, clean npm cache, reinstall) – Iancovici Jan 06 '18 at 19:44
  • I'm stuck on step 1. I added two screenshots of the situation in the description. – annieg4123 Jan 06 '18 at 19:47
  • How did you install nodejs – Iancovici Jan 06 '18 at 19:51
  • I installed Node from their offical website. – annieg4123 Jan 06 '18 at 19:52
  • Recommend resorting to [homebrew approach](https://changelog.com/posts/install-node-js-with-homebrew-on-os-x) very friendly mac installing/update for terminal. Will want to uninstall your current node if possible – Iancovici Jan 06 '18 at 19:55
  • I think the problem may be my $PATH. When I was first encountered the the EACCESS error from installing Angular, I thought the solution was to edit the $PATH and add /usr/local/bin/ to it. Ultimately that didn't work and I ended up resolving it from the website I linked in the description. So my $PATH right now might be messed up. I'm just not sure if that's the reason for the ng command issue. I attached a screenshot of my $PATH. – annieg4123 Jan 06 '18 at 20:02
  • possible duplicate of this https://stackoverflow.com/questions/37227794/ng-command-not-found-while-creating-new-project-using-angular-cli – Asad Shakeel Jul 05 '19 at 12:31

6 Answers6

2

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

Orlov Andrey
  • 262
  • 4
  • 18
1

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
Paul Isaris
  • 414
  • 4
  • 13
1

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 
Iancovici
  • 5,574
  • 7
  • 39
  • 57
1

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)

andreas
  • 226
  • 2
  • 3
0

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.

Jonathan Fuerth
  • 2,080
  • 2
  • 18
  • 21
0

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.

Shreya
  • 69
  • 1
  • 5