What exactly does 'npm install -g ' do?

liam xu picture liam xu · Dec 22, 2015 · Viewed 14.4k times · Source

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.

Answer

Ludovic C picture Ludovic C · Dec 22, 2015

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