Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

APD
APD

Posted on • Originally published atMedium on

     

Ways to fix “-bash: ng: command not found”

What you have already tried

ng version
Enter fullscreen modeExit fullscreen mode

or any otherng commands.

Error produced by the previous command

-bash: ng: command not found
Enter fullscreen modeExit fullscreen mode

Before heading into the fix, let me ask a question

Are you sure that Angular is globally installed in your machine? If yes, you can continue to read this article. Else I recommend you to visit theofficial Angular site and get the latest version installed :)

This problem is caused due to the reason thatnpm is unable to find ng. There are few ways that we can follow to get rid of this error.

1 . Linking angular/cli with npm

This can be done using the command,

npm link @angular/cli
Enter fullscreen modeExit fullscreen mode

2. Setting the location of npm folder

export PATH="$HOME/.npm-global/bin:$PATH"
Enter fullscreen modeExit fullscreen mode

or

export PATH="$HOME/.npm-packages/bin:$PATH"
Enter fullscreen modeExit fullscreen mode

3. Clearing cache and Re-installing Angular (least recommended)

In this case, you may need to uninstall angular from your machine with the command

npm uninstall -g @angular/clinpm cache cleannpm install -g @angular/cli
Enter fullscreen modeExit fullscreen mode

4. Giving an alias to ng path

To do this you must know the full path of your angular installation. For example:“/Users/<username>/.npm-global/lib/node_modules/@angular/cli/bin/ng”. So the command would be

alias ng="/Users/Dharshini/.npm-global/lib/node\_modules/@angular/cli/bin/ng"
Enter fullscreen modeExit fullscreen mode

Note that the above option works for many users but the problem is that this is just a temporary solution. It means that this command works only on your current terminal (same terminal where you ran the above command). Once you close and open a new terminal, the problem seems to reappear.

So how can we permanently solve this? Here comes the solution.Credits:Thanks for this fix.

5 . By adding the PATH directly to the Mac OS terminal

Before doing this we just have to verify whether angular is installed to the base of the user. To check this, in your terminal enter

cd ~
Enter fullscreen modeExit fullscreen mode

to get to the base of your files. Then in order to list all your base files, enter

ls -a
Enter fullscreen modeExit fullscreen mode

Now you must see “.npm-global” listed in the files.

If its not there, again I recommend you to install Angular using the official documentation link I have mentioned in the beginning.

If the folder is listed, you are good to go. Go back to the base of the user using the command

cd ~
Enter fullscreen modeExit fullscreen mode

Here we are going to edit the paths using vim editor. Use “sudo” in front of the command to avoid permission issues. Here you will be prompted to enter the device’s password.

sudo vim /etc/paths
Enter fullscreen modeExit fullscreen mode

The editor now opens a file containing some paths as shown below. All you have to do here is to insert the path of.npm-global/bin to the list. To do so you have to press insert from your mac keyboard (pressfn + i key combination), navigate to the bottom of the list and add ~/.npm-global/bin as I have added below.

/usr/local/bin/usr/bin/bin/usr/sbin/sbin~/.npm-global/bin~ ~
Enter fullscreen modeExit fullscreen mode

Now to save your changes and exit, pressESc key and then type :wq vim command which means to write/save the changes and quit. This will bring you back to the terminal window. Now restart your terminal and type

echo $PATH

This should give you all the paths with the newly added one concatenated at the end.

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:~/.npm-global/bin

Finally the happy ending. Try anyng command

ng version
Enter fullscreen modeExit fullscreen mode

There you go! Thanks for reading this article. Have a great day!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
selimmeske profile image
SelimMeske
Just one ordinary guy who loves solving problems, inventing new things and trying to make the world a better place!
  • Location
    Tuzla
  • Education
    IT
  • Joined

Nice post Angela, ty !

A quick fix that worked for me is using "npm run" before ng.

e.g "npm run ng generate component folder/componentname"

CollapseExpand
 
apdharshi profile image
APD
Front end developer with a passion and focus for design
  • Location
    Sri Lanka
  • Work
    Front end developer
  • Joined

Hi Selim,

Thank you so much for your appreciation and also your positive contribution.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front end developer with a passion and focus for design
  • Location
    Sri Lanka
  • Work
    Front end developer
  • Joined

More fromAPD

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp