What exactly does 'npm install -g ' do?

20.4k Views Asked by At

I want to set up a private npm registry using sinopia and I executed npm install -g sinopia, but some error message occurred:

> [email protected] install /usr/local/lib/node_modules/sinopia/node_modules/crypt3
> node-gyp rebuild

gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/4.2.3"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/sinopia/node_modules/crypt3/.node-gyp"
make: Entering directory `/usr/local/lib/node_modules/sinopia/node_modules/crypt3/build'
  CXX(target) Release/obj.target/crypt3/crypt3.o
In file included from ../crypt3.cc:7:0:
../node_modules/nan/nan.h:261:25: error: redefinition of âtemplate<class T> v8::Local<T> _NanEnsureLocal(v8::Local<T>)â
 NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Local<T> val) {
                         ^
../node_modules/nan/nan.h:256:25: error: âtemplate<class T> v8::Local<T> _NanEnsureLocal(v8::Handle<T>)â previously declared here
 NAN_INLINE v8::Local<T> _NanEnsureLocal(v8::Handle<T> val) {
                         ^
../node_modules/nan/nan.h:661:13: error: ânode::smallocâ has not been declared
     , node::smalloc::FreeCallback callback
             ^

I can see the .h files which relate to C or C++; how come this happens? All the stuff I found within sinopia is about JavaScript.

What does npm install do? In my opinion, it should only initiate some download process.

4

There are 4 best solutions below

2
On BEST ANSWER

npm install <package> or npm install -g <package> will

  1. Download an npm package you specify with the argument, or inside your package.json file, along with its dependencies (from the npm repository host you define) inside a node_modules folder. (Or use an already existing local copy of it. see shrink-wrapping)

  2. Run the pre-install, install and post-install scripts for itself and each of its dependencies. See Lifecycle Scripts

  3. The -g directive tells npm to install the package in the global shared node_modules folder (usually where node is). This will also allow you to access the module from the command-line, as the bin is symlinked into a PATH folder (usually usr/local/bin). Check this link

In the case of sinopia, they do not have a standard package.json file, they have a package.yaml file. Check the yamp plugin.

If you check their pre-publish script, it contains

prepublish: js-yaml package.yaml > package.json

Which converts their package.yaml into package.json. In their package.json, they have a dependency on the crypt3 package.

In the case of crypt3 (one of sinopia dependencies), check the package.json . It contains

  "scripts": {
    "test": "node test/test.js",
    "install": "node-gyp rebuild"
  },

So, when sinopia is npm installed, it will download and install all if its dependencies as well. When crypt3 is installed, the "node-gyp rebuild" will be run, which is why you are seeing native c / c++ compile outputs in your console.

You can try it yourself by doing

npm install -g node-gyp && node-gyp rebuild

In the console

0
On

The g in npm install -g is a flag signifying that you want to install that particular npm module system wide (globally). Without the g option, the module would be installed locally inside the current directory called node_modules -try it!

The location of your globally installed packages can vary depending on how you have installed node. Find out where they are installed by typing npm list -g in your command line.

edit: your error might be caused by insufficient privileges in your npm root directory, but it could also be that the version of node you're using is not supported by that library. Check which version of node you need to run crypt3 and make sure your version of node matches that (node -v). If it does not match the required version, you can use a node version manager such as nvm to switch to that version, and try npm install again.

0
On

npm install -g <package-name> attempts to install the package into a system-wide node_modules directory (for Mac, this would be "/usr/local/lib/node_modules")

0
On

The g flag in npm i -g <package-name> states that, You do not have to install the specific package ever again because it just becomes a shared module of the node