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?
You can do this by using the --prefix flag and the --global* flag.
pje@friendbear:~/foo $ npm install bower -g --prefix ./vendor/node_modules
bower@0.7.0 /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_modulesNPM_CONFIG_PREFIX=./vendor/node_modules$HOME/.npmrc or userconfig param$PREFIX/etc/npmrc or userconfig parampath/to/npm/itself/npmrcBy 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.
After searching for this myself wanting several projects with shared dependencies to be DRYer, I’ve found:
require()require()bin and man paths to $PATHnpm link (info) lets you use a local install as a source for globals→ stick to the Node way and install locally
ref:
I would just install locally, then in your package.json, move it from dependencies section to devDependencies. Depending on what you use to deploy, it might not be deployed (would not on heroku for example).
devDependencies": {
"module": "0.1.0"
}