n: zsh: command not found: n
Problem
I try to install n then use it but it’s not working for me.
Short Version
I followed the installation with npm then when I try to use the command it says : zsh: command not found: n
Long Version
I was on old Node version 10.x I install last LTS Node version on https://nodejs.org/en/ (16.13.2) to start a new project in VueJS, but my old project for my job stop working with this version so I wanted to install n to manage my Node versions
I do :
% npm install -g n
added 1 package, and audited 2 packages in 653ms
found 0 vulnerabilities
then :
sudo mkdir -p /usr/local/n
sudo chown -R $(whoami) /usr/local/n
sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
% ls /usr/local
bin include lib n share
then :
% n
zsh: command not found: n
% which n
n not found
Configuration Details
% n --version
zsh: command not found: n
% command -v node
/usr/local/bin/node
% node -p process.platform
darwin
% echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin
# I'm on macOS Monterey
# MacBook Pro (16 pouces, 2019)
# I have an intel processor, it's not a M1
Ask me if you need more infos
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16
Second story. What about
PATH?The
PATHenvironment variable tells the shell where to look for executable files. When you typenodeornthe shell looks in the list of locations.PATHis a list of folders separated by:.To let you run executables from npm global packages, you need to have their install location in your
PATH. The executable part of the packages is in thebinfolder under where npm put the packages.So with your current setup, you can put a line like this in your shell startup file (e.g.
.zshrcif you are runningzsh):That adds the npm globals folder in the front of the search, and exports it so sub shells see it too.
If you reset your npm configuration and install everything to the default locations, you do not need to alter
PATHbecause/usr/local/binis already included in the search locations by default on Mac.If you tell
nto install Node.js to your home folder, and npm installs global packages to its default location, then you need to add that location to the search path. That is what these lines do in the README. Notice thatbinfolder again.First story. This is for your information, and you don’t need to change anything! (No mention of PATH in this story, see the next story for that.)
If on a fresh computer you install Node.js (node and npm) and then try and install a global package, you get a permission error. e.g.
This is expected behaviour. The default location for
npmis/usr/local/bin/npm. The default location npm uses for global packages is “beside” npm in/usr/local/lib/node_modules. The default permissions are users can not write to this location.So, what to do? Four different approaches.
Just stick
sudoin front of command? No! No! No! This is often suggested because it is so easy but is a bad idea in general. npm installs can run arbitrary scripts, so running them withsudois a bad idea from a security point of view.If it is a personal computer, you might change permission of folders so you can write to the folders used for
npmglobal packages. This is same idea as what Homebrew suggests as part of its setup. (NB: some people do not like changing permissions on system folders which is a valid view. I decided changing permissions was a reasonable compromise on “my” computer for convenience, to use default install locations.)Note: this command you ran was to change the permissions on the folders used by node and npm to allow you to install Node.js and npm packages without using sudo:
Use a node version manager, and install and run node and npm out of your home folder. Then when npm installs the global packages “beside” itself, it is in your home folder. Changing the Node.js install location is what
N_PREFIXis for, and whatn-installdoes for you.Change the prefix so npm install global packages somewhere else. This is what you may have done. e.g.
There have not been many issues around custom npm prefix. You did a good job supplying enough information to help me work out that was the underlying issue.
Glad things are working for you now. Good luck!
(Keeping the old globals in a temp folder is a good paranoid step, to keep round for a while.)
See if you can make sense of enough of all that information, and hopefully it will give you more clues for reading other descriptions on the internet too! 🤯 😄
Approach 1: I suggest using a node version manager AND a custom location for npm global packages is a bit too complex unless you know what you are doing. So you might want to remove the setting for custom npm prefix.
Approach 2: But maybe something in your setup now relies on this, so an easy thing with your current setup is just:
After adding that to your shell startup script,
nshould be a recognised command when you open a new shell and also any other npm installed executables.Approach 3
Yes, this should make
navailable from a location which is already in yourPATH. You will still have the issue that you can’t run binaries you install using npm though.@shadowspawn I was also going to echo in addition that .zshrc needs to be inspected for $PATH configuration.