The previous article "Detailed Explanation of Common NPM Commands in Nodejs" mainly organizes the most commonly used npm commands and related introductions. It mentions global installation (npm install gulp -g) and local installation (npm install gulp). The following figure comes from the Gulp Chinese website introduction guide. It can be seen that after the global installation, gulp will also be installed locally. I was confused at the beginning? Why does gulp installed globally need to be installed locally? Can't we directly call locally installed packages in the project?
With doubts, I found relevant documents on Google. After making all the messes, I found some of the problems. Let us solve some of the doubts about the global installation and local installation of nodejs. If there is something wrong, please pay attention to correct them.
This article uses the Windows platform for testing and uses gulp as an example to make tutorials
What is the global installation location
The global installation method is to type the command: npm install gulp -g or npm install gulp--global, where the parameter -g means to install it into the global environment. The package is installed in the node_modules folder under the Node installation directory. Generally, in the /Users/username/AppData/Roaming/ directory, you can use npm root -g to view the global installation directory.
Call method
After global installation, it can be used by the command line. Users can directly run the commands supported by the component package in the command line. The cmd file after installing gulp globally is shown in the following figure
What is the local installation location
The local installation method is to type the command: npm install gulp or npm install gulp --save-dev, etc., where the parameter --save-dev means to write your installation package information into the devDependencies field of the package.json file, and the package is installed in the node_modules folder of the specified project.
Call method
After local installation, you can directly introduce the modules in the node_modules directory in the project through require(). In the following example, after local installation, require('gulp') directly in gulpfile.js.
Why do I need to install locally after global installation
Is global installation enough
1. In the js instance code, by default, node.js will search for modules in NODE_PATH and the node_modules folder under the current js project. Therefore, if it is just a global installation, the module cannot be directly referenced through require(). It is necessary to manually solve the configuration problem of package paths. Of course, you can also copy the globally installed node_modules folder to the project. There is also a way to choose to set the NODE_PATH of the environment variable to C:/Program Files/nodejs.
2. It is difficult to manage the update of packages. You may need to rename each package, such as [email protected], [email protected]... In order to distinguish the specified packages from different projects, ensure the interdependence between modules (this will be introduced below), and distinguish the normal operation of each project.
Therefore, it is not recommended to install only globally.
Importance of Local Installation
The earliest node.js/npm is actually global, including NODE_PATH compatible, but it cannot support global multi-versions. Therefore, the nodejs team's local installation method may be to ensure the interdependence between different versions of packages. What does it mean? For example, there are 12 interdependence packages in the weui component.
The specified version number of the dependent package is as follows and cannot be modified easily, because the functions of the dependent package versions of different versions of the package are different. If you modify the specified version to run weui, you may have compile errors and other bugs.
Let me give you another example:
The copy code code is as follows: the package version is:
A(0.0.1) depends on B(0.0.2), B(0.0.1) depends on C(0.0.3) After the original author has been updated for a period of time, the package version is: A(1.0.1) depends on B(1.0.0), and B(1.0.0) depends on C(1.0.0)
Each update may bring different functions. It is particularly important to keep the module version information used to download the specified version number when multiple people cooperate, publish the module, and upload it to github for others to use.
Local installation allows each project to have independent packages and is not affected by global packages, which facilitates the movement, copying, packaging, etc. of the project, ensuring the interdependence between different versions of packages. These advantages are difficult to achieve by global installation.
In addition, according to the node team, local installation packages will load faster for the project.
There are advantages and disadvantages. For example, every new project requires local installation of the packages it depends on. The installation package time is relatively long. First, the package is too large and the download is slow; second, it is wasted hard disk space. However, now the computer hard disk only has a few T. Do you still care about saving this space?
The above is the difference between the global installation of Nodejs and local installation introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!