local packages are installed in the directory where you run npm install <package-name> , and they are put in the node_modules folder under this directory. global packages are all put in a single place in your system (exactly where depends on your setup), regardless of where you run npm install -g <package-name>
the package is installed in the current file tree, under the node_modules subfolder.
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.
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
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.
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
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:
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