7

I am trying to run ng build in my project folder, but I get the following response:

bash: ng: command not found

What am I missing? Does it have to do with admin privileges, my path/profile, or something I need to install globally?

This is my package.json:

{
"name": "meanauthapp",
"version": "1.0.0",
"description": "MEAN stack authentication app",
"main": "app.js",
"scripts": {
"start": "node app"
},
"dependencies": {
"express": "4.16.2",
"mongoose": "4.13.6",
"bcryptjs": "2.4.3",
"cors": "2.8.4",
"jsonwebtoken": "8.1.0",
"body-parser": "1.18.2",
"passport": "0.4.0",
"passport-jwt": "3.0.1"
},
"author": "",
"license": "MIT",
"devDependencies": {
  "@angular/cli": "^1.6.0"
  }
}
Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
chody
  • 189
  • 1
  • 1
  • 10

12 Answers12

7

In case others come here with this issue as I have, here is how I solved it system-wide on MacOS. Hope this helps.

Verify node is installed:

$ node -v
v11.2.0

Verify npm is installed:

$ npm -v
6.4.1

Verify your npm global install file path is configured (known as prefix). Mine is under ~/.npm-packages:

$ npm config ls -l | grep prefix
prefix = "/Users/christiangroleau/.npm-packages"

If not, you can place it into your ~/.npmrc file:

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

Verify that your prefix path is listed in your system PATH:

$ echo $PATH
/Users/christiangroleau/.npm-packages/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

If not, invoke the following:

export PATH="$HOME/.npm-packages/bin:$PATH"

Finally, you can reinstall angular-cli (in my case I needed to install it globally):

$ npm install -g @angular/cli

Verify installation:

$ ng -v
Christian Groleau
  • 813
  • 1
  • 12
  • 17
6

try to uninstall and clean the cache.

npm uninstall -g @angular/cli
npm cache clean
npm install -g @angular/cli
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
6

npm doesn't recognize ng command.

Try this command:

npm link @angular/cli

to link npm and angular-cli.

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
5

Try this:

node_modules/.bin/ng build

Or better, add a blank "ng": "ng"line into your scripts key in package.json and run this:

`npm run ng build`

You could also add this to your path:

export PATH=$PATH:./node_modules/bin

Which would let you run any binary in any npm project you might have.

Zlatko
  • 18,936
  • 14
  • 70
  • 123
2

You need to install the Angular CLI globally. Run npm install -g @angular/cli

JSON Derulo
  • 9,780
  • 7
  • 39
  • 56
  • 1
    I already have the Angular CLI installed globally and it still doesn't work. – chody Jul 25 '18 at 05:57
  • @chody then this is not Angular CLI related. It seems like it is a general problem that you can't install global NPM packages. Maybe this helps you: https://stackoverflow.com/questions/15054388/global-node-modules-not-installing-correctly-command-not-found – JSON Derulo Jul 25 '18 at 06:05
  • @chody can you please make sure angular cli is installed using `npm list -g --depth 0`, i loose my global installed packages Everytime I update node. – sn42 Jul 25 '18 at 06:06
  • @sn42 I did that and it returned a lot of errors. What should I do to remedy this? – chody Jul 25 '18 at 06:11
  • npm ERR! error in /usr/local/lib/node_modules/yarnpkg/node_modules/request/node_modules/form-data: E NOENT: no such file or directory, open '/usr/local/lib/node_modules/yarnpkg/node_modules/request/nod e_modules/form-data/package.json' npm ERR! invalid: form-data /usr/local/lib/node_modules/yarnpkg/node_modules/request/node_modules/fo rm-data npm ERR! error in /usr/local/lib/node_modules/yo/node_modules/combined-stream: ENOENT: no such file or directory, open '/usr/local/lib/node_modules/yo/node_modules/combined-stream/package.json' – chody Jul 25 '18 at 06:46
2

For windows user: Just go to your windows environment setting and set user path as C:\Users\HP\AppData\Roaming\npm

2

If you have installed the dependencies locally and need to refer those packages instead of global. use npx before the the command eg: npx ng build refers to the locally installed package. ng build refers to the global package.

Chandan S
  • 143
  • 9
1

I had the same error, but I am running Windows 10 and none of answers worked for me. These were the steps I took to get it resolved:

Verify Angular is installed by running the following command in Git BASH:

npm run ng -v

Install Angular if it is not already installed by running the following command:

npm install -g @angular/cli@latest

Get the npm PATH by running the following command:

npm config get prefix

Open up advanced system settings by either using the shortcut windows+pause (aka break) and clicking on Advanced System Settings or by using Cortana to search for the phrase System Environment Variables

Click on Environment Variables to bring up the environment variables dialog.

Select the row for the user variable Path and click on Edit

Click on New and enter the path returned from earlier. Then click on OK and OK again to close out of the Environment Variables dialog.

Restart Git BASH and test that it working by running the command:

ng -v
David
  • 5,877
  • 3
  • 23
  • 40
0

Based on your comments to other answers: This npm issue might be related to your problem, your npm installation might be outdated.

First of check whether you use a recent version of node. Then update npm with npm i -g npm as the issue suggests (near bottom). Then if ng is still not found follow the update instructions of @angular/cli:

npm uninstall -g @angular/cli
npm cache verify
# if npm version is < 5 then use 'npm cache clean'
npm install -g @angular/cli@latest
sn42
  • 2,353
  • 1
  • 15
  • 27
0

This was a pain to figure out, so here is my solution

1. npm install -g @angular/cli

2. Make sure the ng path is correct
    * cd ~/npm-global/bin
    * ls and make sure ng exists here!
3. create .bashrc or .bash_profile or .bash_everything file on your home directory with:
    * touch ~/.bash_profile 
open this file with text editor or Sublime or vi or etc.
    * open -e .bash_profile 
4. Add ng as alias
    * writing this:
   alias ng="~/npm-global/bin/ng"  or   alias ng="MyFullPAThere/npm-global/bin/ng"
    * save file and close text editor. 
5. Temporarily Update . bash_profile reference
Your terminal wont take bash updates until a restart but the following command will let you use the updates during the terminal session:
    * source ~/.bash_profile
6. Check that ng works
    * ng --version

Hope that helps someone!

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
0

In my case, I have just copied a whole repository (including its node_modules) from a Windows machine to a Linux machine, and running npx ng XXX failed with the following error:

npx: installed 1 in 1.677s
command not found: ng

The main issue was, of course, line breaks. Using the dos2unix package, I was able to overcome it:

sudo apt install -y dos2unix
dos2unix node_modules/.bin/ng
chmod +x node_modules/.bin/ng
Maor
  • 63
  • 2
  • 7
0

If you are on mac and using nvm then just alias you nvm dir node versions like this

alias ng=/Users/your-user/.nvm/versions/node/v14.16.0/lib/node_modules/@angular/cli/bin/ng