Using npm we can install the modules globally using -g option. How can we do this in the package.json file?
Suppose, these are my dependencies in package.json file
"dependencies": {
"mongoose": "1.4.0",
"node.io" : "0.3.3",
"jquery" : "1.5.1",
"jsdom" : "0.2.0",
"cron" : "0.1.2"
}
When i run npm install, I want only node.io to be installed globally, the rest others should be installed locally. Is there an option for this?
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
devDependenciessection of your package.json. Anytime you use something fromscriptsin package.json your devDependencies commands (in node_modules/.bin) act as if they are in your path.For example:
Then in package.json:
Then at your command prompt you can run:
New NEW Note: For awhile now we have had
npx, which allows you to run the devDependencies commands without needing to add them to yourscriptssection (if you want).For example:
But if you really want to install globally, you can add a preinstall in the scripts section of the package.json:
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
npmwhere global Node package installs requiredsudo. One option is to change yournpmconfiguration so this isn’t necessary:npm config set prefix ~/npm, add $HOME/npm/bin to $PATH by appendingexport PATH=$HOME/npm/bin:$PATHto your~/.bashrc.Another, probably better option is to just use
nvmto manage Node and you won’t have that problem.