NPM package installation is divided into local installation (locally), the global installation of two, from the command line to see, the difference is only G, such as
NPM Install Grunt # Local installation NPM install-g GRUNT-CLI # Global Installation
What is the difference between these two types of installation? From the description of the NPM official documentation, the main difference is (explained later by specific examples):
Local Installation
1. Place the installation package under./node_modules (the directory where NPM is running)
2. A locally installed package can be introduced via require ()
Global Installation
1. Place the installation package under/usr/local
2. Can be used directly on the command line
Local Installation
1. Place the installation package under the./node_modules (the directory where NPM is running)
For example, run the following command
NPM Install grunt--save-dev
Then, you will find a node_modules directory in the current directory, you can see grunt this package
Casperchenmacbookpro:testusemin casperchen$ lltotal 200drwxr-xr-x casperchen staff 544B 23:17 node_modules
Enter Node_modules
Casperchenmacbookpro:node_modules casperchen$ lltotal 0drwxr-xr-x casperchen staff 544B 5 00:49 Grunt
2. The locally installed package can be introduced via require ()
Just for example, we create test.js under the project root, which is very simple
var grunt = require (' grunt '); Grunt.log.writeln (' Hello Grunt ');
Then run Test.js on the console
Node Test.js
You will then see the following output
Casperchenmacbookpro:testusemin casperchen$ node Test.js Hello Grunt
Global Installation
1. Place the installation package under the/usr/local
Run the following command
NPM install-g GRUNT-CLI
Then go into the/usr/local/bin directory and you'll see that GRUNT-CLI is already placed below.
Casperchenmacbookpro:bin casperchen$ pwd/usr/local/bincasperchenmacbookpro:bin casperchen$ ll gruntlrwxr-xr-x 1 root Admin 39B 8 21:43 grunt. /lib/node_modules/grunt-cli/bin/grunt
Visible, the real installation path of the global module under/usr/local/lib/node_modules/, the executable file under/usr/local/bin is just a soft link.
2, can be directly used in the command line
Implementation details are described above, and are implemented by creating a soft link under '/usr/local/bin '. I don't repeat it here.
More intuitive examples
Here's a look at what happens when we run the Grunt task (task for a specific grunt task name and replace it yourself) in the project directory. The node-inspector is here to help.
First of all, no contact with node-inspector children's shoes can refer to the previous article to understand the following
Run the following command to turn on debugging
Node-inspector &
See the output below
Casperchenmacbookpro:tmp casperchen$ node-inspector &[1] 14390casperchenmacbookpro:tmp casperchen$ node Inspector v0.6.1 Info-socket.io startedvisit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Next, run the grunt task under the current task
^ccasperchenmacbookpro:testusemin casperchen$ node--debug-brk $ (which grunt) Devdebugger listening on port 5858
Then, open the Chrome browser, enter the URL http://127.0.0.1:8080/debug?port=5858, will automatically enter the breakpoint debugging state. As you can see from the tips shown on the side, the global command grunt is actually/usr/local/lib/node_modules/grunt-cli/bin/grunt
Press F8 and then run down, will enter as Gruntfile.js, at this time the grunt, is a local installation of a node package. The global command, like the local package name, is quite confusing.
[Go] npm install local installation differs from global installation