Nodejs differences between a global installation and a local installation

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/PeunZhang/p/5629329.html

To pay for more than 2 years, from 2 years ago the Internet model to switch to the model, the main scenario is to deal with the offline merchants, unlike the previous Internet model, there are product managers to ask for, we help to solve the problem.

After the transformation, the team members need to find the direction of business, thinking can do something for the merchant or business to reflect the value of their body, such a change is the opportunity is also a challenge, I have always believed in a special environment can quickly hone the cross-job skills and comprehensive quality, But all the way to find their own short board (so-called product thinking), not to promote it can, sometimes feel very incompetent.

So boil over, suggesting that they maintain a positive and optimistic attitude, usually use some common methods to remind themselves, such as ask yourself why do this, to customize some small goals, such as a good mentality, clear thinking, clear goals.

Although not achieved much, but must see themselves in progress, in addition to technological progress, the key is their own ideological progress, ability long in their own body, no one to rob.

Enter text ~

The previous article, "NPM Common commands in detail" is mainly to collate the most commonly used NPM command and related introduction, which refers to the global installation (NPM install gulp-g) and local installation (NPM Install gulp), from the Gulp Chinese network Getting Started guide, It can be seen that after the global installation of Gulp after the installation of the local, at first I was puzzled? Why is the global installation of the Gulp, but also the local installation, not directly in the project to call the locally installed package?

With doubts, Google found the relevant documents, assorted make up a little bit, let us solve the NODEJS global installation and local installation of some doubts, if there is wrong place, also please pay attention to correct.

This article takes a test on the Windows platform and takes gulp as an example tutorial

Directory
    • What is global installation (GLOBALS)
    • What is local installation (locally)
    • Why install locally after a global installation
      • is the global installation sufficient only?
      • Key features of local installation

What is the global installation location

The global installation method is type command: NPM install gulp-g or npm Install Gulp--global, where parameter-G means to install in the global environment, the package is installed in the Node_modules folder in the node installation directory, generally in the \ Users\ User name \appdata\roaming\ directory, you can use NPM root-g to view the global installation directory.

Invocation mode

The global installation can be used by command line, which allows the user to run commands supported by the package directly on the command line, such as a cmd file after the global installation of Gulp

What is the local installation (locally) installation location

The local installation method is type command: NPM Install Gulp or npm Install gulp--save-dev, etc., The meaning of the parameter--save-dev is that the package is installed in the Node_modules folder of the specified project on behalf of the Devdependencies field that writes your installation package information to the Package.json file.

Invocation mode

After the local installation can be directly through the require () to introduce the module in the Project Node_modules directory, the following example, the local installation directly in the Gulpfile.js require (' Gulp ').

Why does the global installation also have to be installed locally only enough for the global installation?

1. In the JS instance code, by default, node. js will be in the Node_path and the current JS under the Project Node_modules folder to look for modules, so if only the global installation, not directly through the require () to refer to the module, The configuration of the package path needs to be resolved manually, of course you can also copy the global installation of the Node_modules folder under the project, there is also a way to choose the environment variable Node_path set to C:\Program Files\nodejs.

2. For the package update is not good management, you may need to rename each package, such as [email protected], [email protected], in order to distinguish between different projects using the specified package, to ensure that the mutual dependency between the modules (this is described below), the difference between the normal operation of each project.

Therefore, only global installation is not recommended.

Importance of Local Installation

The first node.js/npm is actually global, including now also compatible with Node_path, but does not support the global multi-version , so the Nodejs team to the local installation method may be to ensure that the different versions of the dependencies between packages, what does it mean? For example, there are 12 interdependent packages in the Weui component.

The specified version number of the dependent package is as follows, cannot be easily modified, because the different versions of packages corresponding to the version of the package depends on the functionality, if you modify the specified version to run Weui, you may compile errors and other bugs.

Let me give you an example:

The package version is:

A (0.0.1) dependent B (0.0.2), B (0.0.1) relies on C (0.0.3) for a period of time after the original author is updated, the package version is: A (1.0.1) dependent B (1.0.0), B (1.0.0) dependent C (1.0.0)

Each update may have a different function, which is especially important when collaborating with multiple people, releasing modules to the NPMJS community, uploading to GitHub for other people to use, and preserving the version information of the module that can be used to download the specified version number.

Local installation allows each project to have a separate package, not affected by the global package, easy to move, copy, package, and so on, to ensure the interdependence between different versions of packages , these advantages are difficult to achieve global installation.

Also, according to the node team, the local installation package will load faster for the project.

There are advantages and disadvantages, such as each new project to install the dependent package locally, the installation package time is relatively long, one is the package is too large to download slow, and the second is a waste of hard disk space, but now the computer hard drive a few t, you will also care to save this space?

Nodejs differences between a global installation and a local 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.