I want to install latest Node.js version
n latest
installing : node-v14.2.0
mkdir : /usr/local/n/versions/node/14.2.0
mkdir: cannot create directory ‘/usr/local/n’: Permission denied
Error: sudo required (or change ownership, or define N_PREFIX)
Something is wrong with ownership. I tried to fix this
sudo chown -R $(whoami) /usr/local/n
Same error again.
n latest
cp: cannot create directory '/usr/local/lib/node_modules': Permission denied
cp: cannot create regular file '/usr/local/bin/node': Permission denied
cp: cannot create symbolic link '/usr/local/bin/npm': Permission denied
cp: cannot create symbolic link '/usr/local/bin/npx': Permission denied
Permissions in n
/usr/local/n$ ll
total 12
drwxrwxr-x 3 miki root 4096 мај 8 13:29 ./
drwxr-xr-x 11 root root 4096 мај 8 13:29 ../
drwxrwxr-x 3 miki miki 4096 мај 8 13:29 versions/
SOLVED
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
When you run as sudo, npm installs files to the same directory, but now they are owned by root. So this is what happens to absolutely every single person who has ever used npm : install some local packages without issue using npm install foo. install global package using sudo install -g foo-cli without issue.
node -v. If you want to switch to the different version of Node, just type n in the terminal and you should see the Node versions listed. Use arrow keys to choose the version and press enter.
If you're using Windows , there's no choice but to use n , or get a Mac . If you switch node versions frequently (e.g., to test the latest version of a feature locally, while keeping your code compatible in production), you should only use nvm for global module compatibility reasons.
n
command downloads and installs to /usr/local
by default, creating the /usr/local/n
folder with these permissions by default:
drwxr-xr-x root wheel .
drwxr-xr-x root wheel ..
drwxr-xr-x root wheel versions
Add yourself to the wheel
group.
1.1 macOS:
sudo dseditgroup -o edit -a $(whoami) -t user wheel
1.2 GNU/Linux:
sudo usermod -a -G wheel $(whoami)
Allow wheel
members writing permission on that folder:
sudo chmod -R g+w /usr/local/n/
You would need to change where n
stores node
versions ownership:
sudo mkdir -p /usr/local/n && sudo chown -R $(whoami) /usr/local/n/
n
saves node binaries, etc.The n command downloads and installs to /usr/local by default, but you may override this location by defining N_PREFIX.
Source: https://github.com/tj/n#optional-environment-variables
Create a folder, ie: $HOME/.n
mkdir $HOME/.n
Define the environment variable N_PREFIX
adding to your shell initialisation file this line:
2.1. bash (~/.bashrc) or zsh (~/.zshrc):
export N_PREFIX=$HOME/.n
2.2. fish (~/.config/fish/config.fish):
set -x N_PREFIX $HOME/.n
Add the new binary path to your environment's $PATH
:
3.1. bash/zsh:
export PATH=$N_PREFIX/bin:$PATH
3.2. fish:
set -x PATH $N_PREFIX/bin:$PATH
you just need to define the N_PREFIX.
First create the folder where the n data will be saved
mkdir $HOME/.n
then export the N_PREFIX in your .bashrc file
export N_PREFIX=$HOME/.n
if you use fishshell, add this to your .config.fish file: set -x N_PREFIX $HOME/.n
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With