DCR
DCR

Reputation: 15665

Understanding npm install -g option

Just getting started with node. I'm a little confused about the npm install -g option. Could someone tell me if the following is correct:

so for example if installed with the -g option:

var app = express();

and if not installed with the -g option:

var express = require(‘express’);
var app = express();

Upvotes: 2

Views: 1844

Answers (2)

Markus
Markus

Reputation: 597

Quite confusing...

NPM has great documentation. Did you read it?

There is no difference in using the modules. It doesn't change anything if you install global or project-local.

First you have to import/require the module. Then you can use it.

Global installation means every project using the same node installation can require it.

Although there is another dependency if the installation is user-global or system-global.

  • User-global: only projects running by this one user can require.
  • System-global: all projects of all users can require.

Upvotes: 0

Brad
Brad

Reputation: 163232

Installing with -g puts the packages in a location accessible in the path so that the package is available from all applications that require it. Normally, you'll only want to use this option when installing utilities that have their own standalone executables (like WebPack, or the Express CLI).

This has absolutely nothing to do with how packages are loaded in your application. You still need to use require().

NPM is effectively independent from Node.js. It's a package manager that has no bearing at all on how require() works.

Upvotes: 4

Related Questions