NPM install--from a simple example, see the difference between a local installation and a global installation

Source: Internet
Author: User

NPM package installation is divided into local installation (locally), the global installation of two, from the command line to see, the difference is only there -g , such as

npm install grunt # 本地安装npm install -g grunt-cli # 全局安装

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, placing the installation package under./node_modules (the directory where NPM is running)

For example, run the following command

--save-dev

Then, you will find a directory in the current directory, in which you node_modules can see grunt the package

casperchenMacBookPro:testUsemin casperchen$ lltotal 200drwxr-xr-x 16 casperchen staff 544B 12 14 23:17 node_modules

Enternode_modules

casperchenMacBookPro:node_modules casperchen$ lltotal 0drwxr-xr-x 16 casperchen staff 544B 12 5 00:49 grunt
2. The locally installed package can be introduced via require ()

Just for example, we created it under the project root test.js , and the content is simple.

Require(' grunt ');  Grunt.  Log.  Writeln(' Hello Grunt ');          

And then run it in the consoletest.js

node test.js

You will then see the following output

casperchenMacBookPro:testUsemin casperchen$ node test.js hello grunt
Global Installation 1, placing the installation package under/usr/local

Run the following command

npm install -g grunt-cli

Then go /usr/local/bin to the directory and you'll find it's grunt-cli already placed below.

casperchenMacBookPro:bin casperchen$ pwd/usr/local/bincasperchenMacBookPro:bin casperchen$ ll gruntlrwxr-xr-x  1 root admin 39B 8 18 21:43 grunt -> ../lib/node_modules/grunt-cli/bin/grunt

Visible, the real installation path of the global module /usr/local/lib/node_modules/下 , /usr/local/bin under the executable file is just a soft link only

2, can be directly used in the command line

The implementation details are actually mentioned above, implemented by creating a /usr/local/bin soft link under the '. I don't repeat it here.

More intuitive examples

Here's a look directly at what happens when we run the project directory grunt task (task is the specific grunt task name and replace it by itself). We need to use it here node-inspector .

First of all, no contact node-inspector with the child shoes can refer to the previous article to understand the next

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 in 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.

Original: http://www.cnblogs.com/chyingp/p/npm-install-difference-between-local-global.html

NPM install--from a simple example, see the difference between a local installation and a global installation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.