I'm on a unix box where I don't have root access.
I changed my .npmrc file (in my user's root directory) to:
prefix=~/global_npm
Now when I do "npm install -g packagename" it installs inside my global_npm directory. Which is good. And then I gave myself path access to it by updating my .bashrc file with:
export PATH=$PATH:~/global_npm/bin
Do I need to do anything else? I think I need to set NODE_PATH but I'm not sure?
Install Package Globally NPM installs global packages into /<User>/local/lib/node_modules folder. Apply -g in the install command to install package globally.
npmrc or simply using the --prefix will allow you to use npm install -g without sudo .
In npm 1.0, there are two ways to install things: globally —- This drops modules in {prefix}/lib/node_modules , and puts executable files in {prefix}/bin , where {prefix} is usually something like /usr/local .
Sindre Sorhus has a great guide at github.com/sindresorhus/guides which I've reposted here.
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.
mkdir "${HOME}/.npm-packages"
.bashrc
/.zshrc
:NPM_PACKAGES="${HOME}/.npm-packages"
npm
where to store your globally installed package. In your $HOME/.npmrc
file add:prefix=${HOME}/.npm-packages
node
will find them. Add the following to your .bashrc
/.zshrc
:NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
.bashrc
/.zshrc
:PATH="$NPM_PACKAGES/bin:$PATH" # Unset manpath so we can inherit from /etc/manpath via the `manpath` # command unset MANPATH # delete if you already modified MANPATH elsewhere in your config MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
Check out npm-g_nosudo for doing the above steps automagically
NOTE: If you are running OS X, 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
Many setups already expect binaries to be found in ~/.local/bin/
. So this answer follows that convention. Other files will get installed to ~/.local/lib/node_modules/
.
npm
Run:
npm config set prefix '~/.local/'
This modifies ~/.npmrc
to include this line:
prefix=~/.local/
~/.local/bin
exists and is in your PATHRun echo "$PATH"
to have a look at your path. If it does not include ~/.local/bin/
already, you will need to configure your system to include it.
mkdir -p ~/.local/bin echo 'export PATH=~/.local/bin/:$PATH' >> ~/.bashrc
Replace .bashrc
with the configuration file of the shell that you are using.
npm install -g packagename
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