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

Most upvoted comments

Second story. What about PATH?

The PATH environment variable tells the shell where to look for executable files. When you type node or n the shell looks in the list of locations. PATH is 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 the bin folder 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. .zshrc if you are running zsh):

export PATH=/Users/nanimans/.npm-global/bin:$PATH

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 PATH because /usr/local/bin is already included in the search locations by default on Mac.


If you tell n to 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 that bin folder again.

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

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.

$ npm install -g n
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules/n
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules/n
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules/n'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules/n'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules/n'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

This is expected behaviour. The default location for npm is /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.

  1. Just stick sudo in 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 with sudo is a bad idea from a security point of view.

  2. If it is a personal computer, you might change permission of folders so you can write to the folders used for npm global 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:

sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
  1. 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_PREFIX is for, and what n-install does for you.

  2. Change the prefix so npm install global packages somewhere else. This is what you may have done. e.g.

$ npm config set prefix '/Users/nanimans/.npm-global'

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:

export PATH=/Users/nanimans/.npm-global/bin:$PATH

After adding that to your shell startup script, n should be a recognised command when you open a new shell and also any other npm installed executables.

Approach 3

So when you said install n in a different way, you mean without npm so install with Homebrew for example ?

Yes, this should make n available from a location which is already in your PATH. 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.