How to set custom location for local installation of npm package?
Is it possible to specify a custom package destination for npm install
, either through a command flag or environment variable?
By default, npm local installs end up in node_modules
within the current directory, but I want it to install into node_modules
within a different directory, for example vendor/node_modules
. How can I make that happen?
TL;DR
You can do this by using the --prefix
flag and the --global
* flag.
pje@friendbear:~/foo $ npm install bower -g --prefix ./vendor/node_modules
[email protected] /Users/pje/foo/vendor/node_modules/bower
*Even though this is a "global" installation, installed bins won't be accessible through the command line unless ~/foo/vendor/node_modules
exists in PATH
.
TL;DR
Every configurable attribute of npm
can be set in any of six different places. In order of priority:
- Command-Line Flags:
--prefix ./vendor/node_modules
- Environment Variables:
NPM_CONFIG_PREFIX=./vendor/node_modules
- User Config File:
$HOME/.npmrc
oruserconfig
param - Global Config File:
$PREFIX/etc/npmrc
oruserconfig
param - Built-In Config File:
path/to/npm/itself/npmrc
- Default Config: node_modules/npmconf/config-defs.js
By default, locally-installed packages go into ./node_modules
. global ones go into the prefix
config variable (/usr/local
by default).
You can run npm config list
to see your current config and npm config edit
to change it.
PS
In general, npm
's documentation is really helpful. The folders section is a good structural overview of npm and the config section answers this question.
If you want this in config, you can set npm config like so:
npm config set prefix "$(pwd)/vendor/node_modules"
or
npm config set prefix "$HOME/vendor/node_modules"
Check your config with
npm config ls -l
Or as @pje says and use the --prefix
flag