NPM Install the difference between a local installation and a global installation

Source: Internet
Author: User
Tags require node inspector

NPM's package installation is divided into local installation (locals), global installation (globally), and from the command line, the difference is just that there is no-G, such as

NPM Install Grunt # Local Installation

NPM install-g GRUNT-CLI # Global Installation

What's the difference between the two installation methods? The main difference, as illustrated in the NPM official documentation, is the following 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. Put the installation package under/usr/local

2. Can be used directly in the command line

Local Installation

1. Put the installation package under./node_modules (the directory where NPM is running)

Like running the following command

NPM Install grunt--save-dev

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

Casperchenmacbookpro:testusemin casperchen$ LL

Total 200

Drwxr-xr-x Casperchen staff 544B 23:17 node_modules

Enter Node_modules

Casperchenmacbookpro:node_modules casperchen$ LL

Total 0

Drwxr-xr-x Casperchen Staff 544B 5 00:49 Grunt

2, can be introduced by require () to install the local package

As a direct example, we create test.js in the project root directory, which is very simple

var grunt = require (' grunt '); Grunt.log.writeln (' Hello Grunt ');

Then run Test.js on the console

Node Test.js

Then you'll see the following output

Casperchenmacbookpro:testusemin casperchen$ Node Test.js

Hello Grunt

Global Installation

1, put 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 find that GRUNT-CLI has been placed underneath.

Casperchenmacbookpro:bin casperchen$ pwd

/usr/local/bin

Casperchenmacbookpro:bin casperchen$ ll Grunt

Lrwxr-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/,/usr/local/bin executable file is only soft link

2, can be used directly in the command line

Implementation details are actually mentioned above, by creating a soft link under '/usr/local/bin. Don't repeat it here.

A more intuitive example

Let's look directly at what happens when we run the grunt task in the project directory (the task is a specific grunt name and replace it yourself). Here we have to use Node-inspector.

First of all, no contact with Node-inspector's children's shoes can refer to the previous article to understand

Run the following command to turn on debugging

Node-inspector &

See the following output

Casperchenmacbookpro:tmp casperchen$ Node-inspector &

[1] 14390

Casperchenmacbookpro:tmp casperchen$ Node Inspector v0.6.1

Info-socket.io started

Visit 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) dev

Debugger 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. The tips shown from the side can be seen, the global command grunt is actually/usr/local/lib/node_modules/grunt-cli/bin/grunt

Press F8 and then run down, and it will go into the gruntfile.js, where the grunt is a locally installed node package. The global command is as confusing as the local package name.

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.