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?
If you want this in config, you can set npm config like so:
or
Check your config with
npm config ls -l
Or as @pje says and use the
--prefix
flagOn Windows 7 for example, the following set of commands/operations could be used.
Create an personal environment variable, double backslashes are mandatory:
%NPM_HOME%
C:\\SomeFolder\\SubFolder\\
Now, set the config values to the new folders (examplary file names):
npm config set prefix "%NPM_HOME%\\npm"
npm config set cache "%NPM_HOME%\\npm-cache"
npm config set tmp "%NPM_HOME%\\temp"
Optionally, you can purge the contents of the original folders before the config is changed.
Delete the npm-cache
npm cache clear
List the npm modules
npm -g ls
Delete the npm modules
npm -g rm name_of_package1 name_of_package2
TL;DR
You can do this by using the
--prefix
flag and the--global
* flag.*Even though this is a "global" installation, installed bins won't be accessible through the command line unless
~/foo/vendor/node_modules
exists inPATH
.TL;R
Every configurable attribute of
npm
can be set in any of six different places. In order of priority:--prefix ./vendor/node_modules
NPM_CONFIG_PREFIX=./vendor/node_modules
$HOME/.npmrc
oruserconfig
param$PREFIX/etc/npmrc
oruserconfig
parampath/to/npm/itself/npmrc
By default, locally-installed packages go into
./node_modules
. global ones go into theprefix
config variable (/usr/local
by default).You can run
npm config list
to see your current config andnpm 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.For OSX, you can go to your user's
$HOME
(probably /Users/yourname/) and, if it doesn't already exist, create an.npmrc
file (a file that npm uses for user configuration), and create a directory for your npm packages to be installed in (e.g., /Users/yourname/npm). In that .npmrc file, set "prefix" to your new npm directory, which will be where "globally" installed npm packages will be installed; these "global" packages will, obviously, be available only to your user account.In .npmrc:
prefix=${HOME}/npm
Then run this command from the command line:
npm config ls -l
It should give output on both your own local configuration and the global npm configuration, and you should see your local prefix configuration reflected, probably near the top of the long list of output.
For security, I recommend this approach to configuring your user account's npm behavior over chown-ing your
/usr/local
folders, which I've seen recommended elsewhere.After searching for this myself wanting several projects with shared dependencies to be DRYer, I’ve found:
require()
require()
bin
andman
paths to$PATH
npm link
(info) lets you use a local install as a source for globals→ stick to the Node way and install locally
ref: