The -save and -save-dev parameters of NPM install are not used properly. In fact, bloggers had a vague understanding of these two parameters before, and had a slight understanding of the similarities and differences between them after various materials and practices. Then write this article to avoid their forget, but also to node ape friends a point of guidance.
When using NPM install to install modules, we usually use the following command forms:
NPM install -g moduleName # -g moduleName # -g moduleName # -g The specific viewing method is to enter NPM config Edit in the terminal and find it in the popup document. Prefix =C:\node, then. NPM install-save moduleName # --save means to install the module into the project directory and write dependencies in the Dependencies node of the package file. NPM install-save-dev moduleName # --save-dev installs the module into the project directory and writes dependencies to devDependencies in the package file. NPM install -g moduleName # -g moduleName # -g moduleName # -g The specific viewing method is to enter NPM config Edit in the terminal and find it in the popup document. Prefix =C:\node, then. NPM install-save moduleName # --save means to install the module into the project directory and write dependencies in the Dependencies node of the package file. NPM install-save-dev moduleName # --save-dev installs the module into the project directory and writes dependencies to devDependencies in the package file.Copy the code
Which of the four commands should we use in a project? It all depends. Here’s a comparison of these four commands so you don’t have to ask them again.
NPM install moduleName command
- Install the module to the project node_modules directory.
- Module dependencies are not written to devDependencies or dependencies nodes.
- Modules are not downloaded when you run NPM Install to initialize the project.
NPM install -g moduleName command
- Installing modules globally does not save module packages in the project node_modules directory.
- Module dependencies are not written to devDependencies or dependencies nodes.
- Modules are not downloaded when you run NPM Install to initialize the project.
NPM install -save moduleName command
- Install the module to the project node_modules directory.
- Writes module dependencies to the Dependencies node.
- When NPM Install is run to initialize the project, the modules are downloaded to the project directory.
- Running NPM install –production or specifying the NODE_ENV variable as production automatically downloads modules to the node_modules directory.
NPM install-save-dev moduleName command
- Install the module to the project node_modules directory.
- Writes module dependencies to the devDependencies node.
- When NPM Install is run to initialize the project, the modules are downloaded to the project directory.
- Running NPM install –production or specifying NODE_ENV as production does not automatically download modules to node_modules.
The modules in devDependencies include gulp and js compression modules. These modules are not needed after our project is deployed, so we can install them using –save-dev. Modules like Express are required to run the project and should be installed under the Dependencies node, so we should use –save.
The main difference is that in the production environment, NPM install — Production installs the module in the Dependencies section, and in the development environment, NPM I installs all modules in devDependencies and dependencies
Therefore, in the production environment, there is no need to install gulp, Webpack and other modules, which can be distinguished by this method