Reputation: 22697
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?
Upvotes: 228
Views: 276459
Reputation: 9596
We can set the default npm resource download path by this command :
npm config set registry https://registry.npmjs.org/
I added registry=https://registry.npmjs.org/ path to the .npmrc file of my react project root path and install my needed package with --force :
npm install react-c3js --force
Also we can comment npm resource path in this file :
Upvotes: -1
Reputation: 22697
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
.
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
or userconfig
param$PREFIX/etc/npmrc
or userconfig
parampath/to/npm/itself/npmrc
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.
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.
Upvotes: 213
Reputation: 406
On 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
Upvotes: 9
Reputation: 921
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.
Upvotes: 16
Reputation: 38418
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
Upvotes: 39
Reputation: 2678
After searching for this myself wanting several projects with shared dependencies to be DRYer, I’ve found:
require()
require()
bin
and man
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:
Upvotes: 7