2

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:

  • npm install -g installs global packages in a general location
  • npm install installs packages in the specific folder
  • regardless of how installed all dependencies need to be listed in the json file
  • dependencies not installed with the -g option need to be listed as required in the appropriate *.js file

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(); 

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

But this is an old post. Nowadays we use import from instead of require. Otherwise, this answer is better stated than the previous one, and of course better than the uncomprehensible documentation not meant for the layman programmer.
0

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.

4 Comments

Read the docs but don't understand them. Could you explain in greater detail what you mean by ... Global installation means every project using the same node installation can require it. I'm running node on localhost, is the same installation just mean the machine or the machine and port number?
It has to do with the runtime environment. NPM is looking up files (.npmrc) and directories (node_modules) based on the installation path of Node/NPM itself and the HOME/USERPROFILE. There is nothing network related in global.
@DCR Neither Node or npm know or care about localhost or port numbers. If you do npm install foo for a project then only that project and nothing else can use that package (with require('foo')). If you do npm install -g foo then every project on your computer can use that package (again with require('foo')).
The docs in the first link you gave lack the -g in the command options. But if you scroll down or search for -g, there's a section about global options.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.