简体   繁体   中英

npm command not found after restart

The npm command keeps becoming unrecognised after a computer restart:

-bash: npm: command not found

The node command still works fine, it's just npm that's not working. I have to reinstall Node.js from the main site to get it working again.

~/bin is added to my $PATH in my .bash_profile with:

export PATH="$HOME/bin:$PATH"

Edit: I'm also using the full mathiasbynens/dotfiles which manages its own version, I wonder if there's a conflict here.

I have no explanation, but a few pointers:

As @LaurentPerrin states, node (via its official installer package) installs to /usr/local/bin .
(Why do you mention ~/bin ?)

Note that different rules apply if you use user-level installs via node.js version manager nvm , in which case [symlinks to] executables are placed in ~/.nvm/v{version}/bin

Thus, the node executable should be /usr/local/bin/node .

Is this (only) what you see when you run which -a node ?

The node executable is placed directly in /usr/local/bin , whereas npm is merely a symlink that points to /usr/local/lib/node_modules/npm/bin/npm-cli.js , which is an executable node script with shebang line #!/usr/bin/env node .

When you run

ls -l /usr/local/bin/npm

you should see something like

lrwxr-xr-x  1 root  wheel  38 Dec 13 11:52 /usr/local/bin/npm -> \
                                           ../lib/node_modules/npm/bin/npm-cli.js

See if the symlink exists and points to the right file.

If that still doesn't work, try to invoke npm-cli.js directly and see what happens (this should show npm's command-line help):

/usr/local/lib/node_modules/npm/bin/npm-cli.js help

According to nvm readme file , try to run this once:

nvm alias default stable

This will set a default Node version to be used in any new shell.

On OSX, node is installed to /usr/local/bin , which needs to be in your path. Edit /edit/paths as root ( sudo nano /etc/paths ) and make sure it is on the first line. It should look like this:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

Then you can force bash to refresh paths by running: source /etc/profile .

I was having the same problem (on OSX). Not sure if this is the solution for you, but I ended up finding npm here:

.nvm/v0.10.22/bin

or, based on your version

.nvm/v0.10.21/bin

Then I just printed my working directory and added that to my path. So for me, when I opened terminal:

cd .nvm/v0.10.22/bin
pwd
>> /path/to/.nvm/v0.10.22/bin

Hope that helps.

I use nvm to install node.js. I solve it by adding $HOME/.nvm/v0.10.25/bin to $PATH . So the problem is that the path can not be found.

I have the same problem. My solution is use sudo ... . My guess this is because of the permission problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM