What exactly does 'npm install -g ' do?

混江龙づ霸主 提交于 2020-02-28 07:56:31

问题


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

> crypt3@0.1.8 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.


回答1:


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

  1. Download a npm package you specify with the argument, or inside your package.json file, along with it's 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 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 it's dependencies as well. When crypt3 is installed, the "node-gyp rebuild" will be run, that's 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




回答2:


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.




回答3:


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")



来源:https://stackoverflow.com/questions/34408154/what-exactly-does-npm-install-g-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!