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?
You could use a separate file, like
npm_globals.txt
, instead ofpackage.json
. This file would contain each module on a new line like this,Then in the command line run,
Check that they installed properly with,
As for whether you should do this or not, I think it all depends on use case. For most projects, this isn't necessary; and having your project's
package.json
encapsulate these tools and dependencies together is much preferred.But nowadays I find that I'm always installingcreate-react-app
and other CLI's globally when I jump on a new machine. It's nice to have an easy way to install a global tool and its dependencies when versioning doesn't matter much.And nowadays, I'm using
npx
, an npm package runner, instead of installing packages globally.All modules from package.json are installed to ./node_modules/
I couldn't find this explicitly stated but this is the package.json reference for NPM.
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 fromscripts
in 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:
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
npm
where global Node package installs requiredsudo
. One option is to change yournpm
configuration so this isn't necessary:npm config set prefix ~/npm
, add $HOME/npm/bin to $PATH by appendingexport PATH=$HOME/npm/bin:$PATH
to your~/.bashrc
.Due to the disadvantages described below, I would recommend following the accepted answer:
My original but not recommended answer follows.
Instead of using a global install, you could add the package to your
devDependencies
(--save-dev
) and then run the binary from anywhere inside your project:In your case:
This engineer provided an
npm-exec
alias as a shortcut. This engineer uses a shellscript calledenv.sh
. But I prefer to use$(npm bin)
directly, to avoid any extra file or setup.Although it makes each call a little larger, it should just work, preventing:
sudo
Disadvantages:
$(npm bin)
won't work on Windows.npm bin
folder. (Install npm-run or npm-which to find them.)It seems a better solution is to place common tasks (such as building and minifying) in the "scripts" section of your
package.json
, as Jason demonstrates above.This is a bit old but I ran into the requirement so here is the solution I came up with.
The Problem:
Our development team maintains many .NET web application products we are migrating to AngularJS/Bootstrap. VS2010 does not lend itself easily to custom build processes and my developers are routinely working on multiple releases of our products. Our VCS is Subversion (I know, I know. I'm trying to move to Git but my pesky marketing staff is so demanding) and a single VS solution will include several separate projects. I needed my staff to have a common method for initializing their development environment without having to install the same Node packages (gulp, bower, etc.) several times on the same machine.
TL;DR:
Need "npm install" to install the global Node/Bower development environment as well as all locally required packages for a .NET product.
Global packages should be installed only if not already installed.
Local links to global packages must be created automatically.
The Solution:
We already have a common development framework shared by all developers and all products so I created a NodeJS script to install the global packages when needed and create the local links. The script resides in "....\SharedFiles" relative to the product base folder:
Now if I want to update a global tool for our developers, I update the "packages" object and check in the new script. My developers check it out and either run it with "node npm-setup.js" or by "npm install" from any of the products under development to update the global environment. The whole thing takes 5 minutes.
In addition, to configure the environment for the a new developer, they must first only install NodeJS and GIT for Windows, reboot their computer, check out the "Shared Files" folder and any products under development, and start working.
The "package.json" for the .NET product calls this script prior to install:
Notes
Note the script reference requires forward slashes even in a Windows environment.
"npm ls" will give "npm ERR! extraneous:" messages for all packages locally linked because they are not listed in the "package.json" "dependencies".
Edit 1/29/16
The updated
npm-setup.js
script above has been modified as follows:Package "version" in
var packages
is now the "package" value passed tonpm install
on the command line. This was changed to allow for installing packages from somewhere other than the registered repository.If the package is already installed but is not the one requested, the existing package is removed and the correct one installed.
For reasons unknown, npm will periodically throw an EBUSY error (-4082) when performing an install or link. This error is trapped and the command re-executed. The error rarely happens a second time and seems to always clear up.