What is the NPM
NPM (Node Pakage Manage) is a third-party package manager and the default package management tool of node.js platform.
NPM do
NPM allows you to install, share, distribute code, and manage project dependencies.
Example:
- NPM install is used to install many third-party modules that are required when configuring WebPack
- In the actual project work, the project needs to be split when the project is large, but many basic tools, methods and functions can be shared. We can put the public code in the corresponding private database provided by our company, and update the database according to the development of business. Each project to update the corresponding version number to reinstall the code can be synchronized updates, thus avoiding repetitive work
NPM inatsll process
- Check whether the node_modules directory exists. If it does not (check /usr/local/bin on your PC), proceed
- NPM queries Registry for the url of the module zip package
- Download the compressed package and save it to the /. NPM directory globally
- At the same time, unzip it to the node_modules directory of the current project
NPM install is different in several ways
NPM install XXX # -g install XXX # -g install XXX # -g install XXX # NPM install --save XXX # --save NPM install --save-dev XXX # --save-dev install the module to the project directory and write dependencies to devDependencies in the package file.Copy the code
npm install X -g
- 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 X
- The X package will be installed in the node_modules directory
- Package.json will not be modified
- X is also not automatically installed when NPM install is run after removing the project node_modules folder
npm install X –save
Development and production environments require such dependencies as JS frameworks, UI libraries, network request libraries, and code execution
- The X package will be installed in the node_modules directory
- Add X to the Dependencies property of package.json
- Then when you run the NPM install command, X is automatically installed into the node_modules directory
- After running NPM install –production or specifying the value of NODE_ENV as production, it will be automatically installed into node_modules, even when the environment is running online
NPM install X – save – dev
Only the development environment needs: for example, the related plug-ins of WENpack, packaging the dependencies needed during compilation
- The X package will be installed in the node_modules directory
- Add X to the devDependencies property of package.json
- Then when you run the NPM install command, X is automatically installed into the node_modules directory
- X will not be automatically installed into node_modules when you run NPM install — production or specify NODE_ENV as production
summary
Use — save for packages needed at runtime, and — save-dev otherwise.
NPM run process
- The script configuration in package.json file corresponds to the command XXX
- NPM run XXX creates a script shell by executing the above configuration command
- Before the script is executed, environment variables are added to the global environment and the corresponding path is deleted after the script is executed
- To use the development package dev as an example, run config-dev.js
- The configuration file starts with the Entry file, loads the dependencies from top to bottom, executes the script, and then packs them into a package
What is NPX when some colleagues use NPX?
- It functions the same as NPM Run
- Small difference, NPM automatically installs dependencies if it doesn’t find them and then removes them
- I didn’t get any deeper than that
Finally, if you think this article is helpful, please remember to like three lian Oh, thank you very much