New Note: You probably don't want or need to do this. What you probably want to do is just put those types of command dependencies for build/test etc. in the devDependencies
section of your package.json. Anytime you use something from scripts
in package.json your devDependencies commands (in node_modules/.bin) act as if they are in your path.
For example:
npm i --save-dev mocha # Install test runner locally
npm i --save-dev babel # Install current babel locally
Then in package.json:
// devDependencies has mocha and babel now
"scripts": {
"test": "mocha",
"build": "babel -d lib src",
"prepublish": "babel -d lib src"
}
Then at your command prompt you can run:
npm run build # finds babel
npm test # finds mocha
npm publish # will run babel first
New NEW Note: For awhile now we have had npx
, which allows you to run the devDependencies commands without needing to add them to your scripts
section (if you want).
For example:
npx webpack
But if you really want to install globally, you can add a preinstall in the scripts section of the package.json:
"scripts": {
"preinstall": "npm i -g themodule"
}
So actually my npm install executes npm install again .. which is weird but seems to work.
Note: you might have issues if you are using the most common setup for npm
where global Node package installs required sudo
. One option is to change your npm
configuration so this isn't necessary:
npm config set prefix ~/npm
, add $HOME/npm/bin to $PATH by appending export PATH=$HOME/npm/bin:$PATH
to your ~/.bashrc
.
Another, probably better option is to just use nvm
to manage Node and you won't have that problem.
The npm install
command will install the devDependencies
along other dependencies
when run inside a package directory, in a development environment (the default).
Use npm install --only=prod
(or --only=production
) to install only dependencies
, and not devDependencies,
regardless of the value of the NODE_ENV
environment variable.
Source: npm docs
Note: you may also need --no-optional
Note: Before v3.3.0 of npm (2015-08-13), the option was called --production
, i.e. npm install --production
.
Best Answer
If you want to do the opposite (i.e. move a module from
dependencies
todevDependencies
) just do:or shorthand:
shorthand to move from
devDependencies
todependencies
(prod):