[Node] [npm] Install npm packeage on MacOS / Linux without sudo

Install npm packages globally without sudo on macOS and Linux

npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package>) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo) to be able to install globally.

Here is a way to install packages globally for a given user.

  1. Create a directory for global packages
mkdir "${HOME}/.npm-packages"
  1. Tell npm where to store globally installed packages
npm config set prefix "${HOME}/.npm-packages"
  1. Ensure npm will find installed binaries and man pages

Add the following to your .bashrc/.zshrc:

NPM_PACKAGES="${HOME}/.npm-packages"

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

# Preserve MANPATH if you already defined it somewhere in your config.
# Otherwise, fall back to `manpath` so we can inherit from `/etc/manpath`.
export MANPATH="${MANPATH-$(manpath)}:$NPM_PACKAGES/share/man"

If you're using fish, add the following to ~/.config/fish/config.fish:

set NPM_PACKAGES "$HOME/.npm-packages"

set PATH $NPM_PACKAGES/bin $PATH

set MANPATH $NPM_PACKAGES/share/man $MANPATH  

If you have erased your MANPATH by mistake, you can restore it by running set -Ux MANPATH (manpath -g) $MANPATH once. Do not put this command in your config.fish.


Check out npm-g_nosudo for doing the above steps automagically


NOTE: If you are running macOS, the .bashrc file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as .profile or .bash_profile. These files also reside in the user's home folder. In this case, simply adding the following line to them will instruct Terminal to also load the .bashrc file:

source ~/.bashrc

See also: npm's documentation on "Fixing npm permissions".

Original

https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md

Recommended Posts

[Node] [npm] Install npm packeage on MacOS / Linux without sudo
Install scipy on Linux without internet connection
Install Minecraft on Arch Linux
Install Linux on your Chromebox
Install the JDK on Linux
Install tomcat 5.5 on Amazon Linux.
Install Homebrew on Amazon Linux 2
Install strongSwan 5.9.1 on Amazon Linux 2
Install Python Pillow on Amazon Linux
Install oracle java8 on amazon linux2
Install CUDA on Linux Mint Mate 20
Install Arch Linux on DeskMini A300
How to install graph-tool on macOS
[Linux] Install Python 3 without administrator privileges
How to install VMware-Tools on Linux
Install pyenv on EC2 (Amazon Linux)
Install AWS CLI v2 without sudo
[Note] Install Imagick on Amazon Linux2
Install PyStan on Windows without Anaconda
Install Java2Python on macOS High Sierra (10.13)
I'll install Ruby on EC2 (Amazon Linux2) 2020
How to install aws-session-manager-plugin on Manajro Linux
Install wsl2 and master linux on windows
Install and launch k3s on Manjaro Linux
Install and Configure TigerVNC server on Linux
How to install packages on Alpine Linux
5 reasons to install Linux on your laptop.
How to install Anisble on Amazon Linux 2
Install Python less than 3.5.3 on macOS Catalina
Dockerfile: Install Docker on your Linux server
Install Mecab on Linux (CentOS) with brew
Run Keycloak on Amazon Linux 2 without Docker
How to install php7.4 on Linux (Ubuntu)
Install rJava on Linux in R3.6 environment.
Until you install Arch Linux on VMware
Install Python 3.8, Pip 3.8 on EC2 (Amazon Linux 2)
Linux "Install on / dev / sda" error resolution
How to install cx_Oracle on macOS Sierra
Install debian on linux on Arrows tab Q584 / H
Install Anydesk Linux version on Chromebook [Result NG]
Install Python 3.8.6 on macOS Big Sur using pyenv
Run pip install on MacOS Python 3.7 or later
Install Python 3 on MacOS Catalina (with Homebrew only)
How to install Camunda Modeler on Manjaro Linux
Install PHP 7 series on Amazon Linux 2 with Amazon Linux Extras
Install Python and libraries for Python on MacOS Catalina