Nodejs global installation is different from local installation.

Source: Internet
Author: User

Nodejs global installation is different from local installation.

In the previous article, "detailed explanation of common npm commands in Nodejs" mainly sorts out the most commonly used npm commands and related introductions, including global installation (npm install gulp-g) and local installation (npm install gulp), from the Getting Started Guide of Gulp Chinese network, we can see that after global installation of gulp, we need to perform local installation. At first I am confused? Why does the global installation of gulp require local installation? Isn't it possible to directly call the locally installed package in the project?

With some questions, I found the relevant documents on google, and I got a little confused when I joined them. Let's solve some questions about nodejs global installation and local installation. If there is anything wrong, please pay attention to your corrections.

This article is a test on the Windows platform and a tutorial on gulp.

What is the global installation location?

The global installation method is to enter the command npm install gulp-g or npm install gulp -- global. The parameter-g indicates that it is installed in the global environment, the package is installed in the node_modules folder under the Node installation directory. Generally, it is in the \ Users \ USERNAME \ AppData \ Roaming \ directory. You can use npm root-g to view the global installation directory.

Call Method

The global installation can be used by the command line. You can directly run commands supported by this component package on the command line, such as the cmd file after the global installation of gulp.

What is the local installation location?

The local installation method is to enter the command npm install gulp or npm install gulp -- save-dev, etc. The parameter -- save-dev indicates that your installation package information is written to the package. in the devDependencies field of the json file, the package is installed in the node_modules folder of the specified project.

Call Method

After local installation, you can directly use require () to introduce the modules under the node_modules directory of the project. For example, after local installation, directly use require ('gulp') in gulpfile. js ').

Why do I need to install it locally after global installation?

Is global installation enough?

1. in the js instance code, the default node. js will search for modules in the NODE_PATH and node_modules folders of the current js project. Therefore, if it is only for global installation, it cannot be referenced directly by require, you need to manually solve the package path configuration problem. Of course, you can also copy the globally installed node_modules folder to the project. You can also choose to set the NODE_PATH of the environment variable to C: \ Program Files \ nodejs.

2. poor management of package updates, maybe you need to rename each package, such as gulp@3.8.1, gulp@3.9.1 ..., to distinguish between different projects and use the specified package, ensure the mutual dependencies between modules (This section will be described below), and distinguish the normal operation of each project.

Therefore, global installation is not recommended.

Importance of local Installation

The earliest node. js/npm is actually global, including it is still compatible with NODE_PATH, but it cannot support global multi-version, therefore, the nodejs team may change to a local installation method to ensure the dependency between packages of different versions. What does this mean? For example, there are 12 packages that depend on each other in weui components?

The specified version number of the dependent package is as follows, which cannot be easily modified. Because different versions of the dependent package have different functions, if you modify the specified version to run weui, it may cause compilation errors and other bugs.

Another example:

Copy codeThe Code is as follows:
A (0.0.1) depends on B (0.0.2), B (0.0.1) depends on C (0.0.3) after the original author updates for A period of time, the package version is: A (1.0.1) depends on B (1.0.0 ), B (1.0.0) depends on C (1.0.0)

Each update may bring different functions. When many people work together, publish modules to the npmjs community, and upload them to github for use by others, it is particularly important to retain the version information of a module for downloading a specified version number.

Local installation allows each project to have independent packages, which are not affected by global packages. It facilitates project movement, copying, and packaging, and ensures the dependency between packages of different versions, these advantages are hard to achieve by global installation.

In addition, according to the node team, the local installation package can load the project faster.

There are also some advantages and disadvantages. For example, each new project requires local installation of the dependent package. The installation package takes a relatively long time. First, the package is too large, leading to slow downloading. Second, the hard disk space is wasted, but now the hard drive on the computer is just a few TB. Do you still care about saving this space?

The above section describes the differences between Nodejs global installation and local installation. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.