Code Whisperer
Code Whisperer

Reputation: 23662

Node NPM - install versus install -g

I am a node newbie and am somewhat confused with the whole "install" thing.

What is the difference between install, and install -g?

Can something installed with install -g be accessed anywhere, or does this make it available to the Node server but not your application? Is there any reason to use one, and not the other?

Cheers

Upvotes: 1

Views: 299

Answers (3)

Vinay Aggarwal
Vinay Aggarwal

Reputation: 1585

The only difference is npm install mod will install it in your local directory. Let's say you are working in 'projectA' directory. So

> npm install mod

will install "mod" in

> projectA/node_modules/mod/

so any .js file inside projectA can use this module by just saying require('mod')

whereas 'npm install mod -g` will install it globally in the user's node module directory. It will be somewhere in

> /usr/bin/npm/node_modules/modA

you can use this module anywhere in any of your project, in addition to that if there is any terminal command is there in 'modA'. It will be accessible from you terminal directory.

> modA --version
> version 1.1

Upvotes: 1

Terry
Terry

Reputation: 14219

install means the module will be created in your local node_modules folder which is highly recommended for anything your application relies on (for versioning, amongst other reasons).

install -g means install the module globally on your machine. This is generally only recommended to use with modules that perform some task unrelated to the execution of your application.

Simple examples of this are Yeoman generators, the Express generator, PhantomJS, etc.

There is an official blog post about it here

Upvotes: 2

Jordan Foreman
Jordan Foreman

Reputation: 3888

From the node.js blog:

  • If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.

  • If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

So for example, lets say you wanted to install the Grunt CLI. Odds are you'll use Grunt in multiple projects, so you'll want to install that globally using -g.

Now lets say you're working on a project and your project is going to require a module such as Express. You would cd to your projects root directory and install the module without -g.

Here is a more in depth explanation.

Upvotes: 4

Related Questions