What is npm
The full name of NPM is Node Package Manager, which is a package management and distribution tool installed with NodeJS. It is very convenient for JavaScript developers to download, install, upload and manage installed packages.
npm install install module
Basic syntax
npm install (with no args, in package dir)npm install [<@scope>/]<name>npm install [<@scope>/]<name>@<tag>npm install [<@scope>/]<name>@<version>npm install [<@scope>/]<name>@<version range>npm install <tarball file>npm install <tarball url>npm install <folder>alias: npm icommon options: [-S|--save|-D|-save-dev|-O|--save-optional] [-E|--save-exact] [-dry-run]
Install the package, the latest version will be installed by default
Copy the code as follows: npm install gulp
Install the specified version
Copy the code as follows: npm install [email protected]
Install the package and keep the information in the project's package.json file
The project's dependence on modules can be represented by the following 3 methods (assuming the current version number is 1.1.0):
Newly released patch versions of compatible modules: ~ 1.1.0, 1.1.x, 1.1 compatible modules newly released small versions and patch versions: ^ 1.1.0, 1.x, 1 compatible modules newly released large versions, small versions and patch versions: *, x
-S, --save The installation package information will be added to dependencies (dependencies in the production stage)
Copy the code as follows: npm install gulp --save or npm install gulp -S
The dependencies field of the package.json file:
Copy the code as follows: "dependencies": { "gulp": "^3.9.1"}
-D, --save-dev The installation package information will be added to devDependencies (dependencies in the development stage), so it is generally used in the development stage.
Copy the code as follows: npm install gulp --save-dev or npm install gulp -D
The devDependencies field of the package.json file:
Copy the code as follows: "devDependencies": { "gulp": "^3.9.1"}
-O, --save-optional The installation package information will be added to optionalDependencies (dependencies for optional stages)
Copy the code as follows: npm install gulp --save-optional or npm install gulp -O
The optionalDependencies field of the package.json file:
The code copy is as follows: "optionalDependencies": { "gulp": "^3.9.1"},
-E, --save-exact Exact installation of specified module versions
Copy the code as follows: npm install gulp --save-exact or npm install gulp -E
Enter the command npm install gulp -ES, and pay attention to the dependencies field of the package.json file to see that the ^ in the version number is gone
Copy the code as follows: "dependencies": { "gulp": "3.9.1"}
After the module's dependencies are written to the package.json file, others open the project's root directory (project open source, internal teamwork) and use the npm install command to install all dependencies configuration according to dependencies configuration.
Copy the code as follows: npm install
Local installation (local)
Copy the code as follows: npm install gulp
Global installation (g), using -g or --global
Copy the code as follows: npm install gulp -g
npm ls view installed module
Basic syntax
Copy the code as follows: npm ls [[<@scope>/]<pkg> ...]aliases: list, la, ll
View globally installed modules and dependencies
Copy the code as follows: npm ls -g
npm uninstall uninstall module
Basic syntax
Copy the code as follows: npm uninstall [<@scope>/]<pkg>[@<version>]... [-S|--save|-D|--save-dev|-O|--save-optional]aliases: remove, rm, r, un, unlink
If you uninstall the development version of the module
Copy the code as follows: npm uninstall gulp --save-dev
npm update update module
Basic syntax
Copy the code as follows: npm update [-g] [<pkg>...]
npm outdated checks whether the module is outdated
Basic syntax
Copy the code code as follows: npm outdated [[<@scope>/]<pkg> ...]
This command lists all outdated packages and can update packages in a timely manner
npm init boots the creation of a package.json file in the project
The information of the installation package can be kept in the project's package.json file for subsequent development of other projects or for cooperation with others. It is also said that package.json is essential in the project.
Copy the code as follows: npm init [-f|--force|-y|--yes]
npm help view detailed help for a command
For example, enter npm help install,系统在默认的浏览器或者默认的编辑器中打开本地nodejs安装包的文件/nodejs/node_modules/npm/html/doc/cli/npm-install.html
Copy the code as follows: npm help <command>
npmroot view package installation path
Output path to node_modules
Copy the code as follows: npm root [-g]
npm config manages npm configuration path
Basic syntax
Copy the code as follows: npm config set <key> <value> [-g|--global]npm config get <key>npm config delete <key>npm config listnpm config editnpm get <key>npm set <key> <value> [-g|--global]
For the most config, you should set up a proxy to solve the problem of failure to install some modules in npm.
For example, I am in the company's intranet, because of the company's firewall, I cannot complete the installation of any modules. Setting up a proxy can solve the problem.
Copy the code as follows: npm config set proxy=http://dev-proxy.oa.com:8080
Another example is the domestic network environment problem, a certain official IP may be harmonious. Fortunately, there are kind-hearted people in China who built a mirror. At this time, we simply set up the mirror.
Copy the code as follows: npm config set registry="http://r.cnpmjs.org"
npm cache management module cache
Basic syntax
Copy the code as follows: npm cache add <tarball file>npm cache add <folder>npm cache add <tarball url>npm cache add <name>@<version>npm cache ls [<path>]npm cache clean [<path>]
The most common command is to clear npm local cache
Copy the code as follows: npm cache clean
npm start start module
Basic syntax
Copy the code as follows: npm start [-- <args>]
This command is written in the start field of the scripts in the package.json file. You can customize the command to configure a server environment and install a series of necessary programs, such as
Copy the code as follows: "scripts": { "start": "gulp -all"}
If the package.json file does not set start, node server.js will be started directly
npm stop stop module
Basic syntax
Copy the code as follows: npm stop [-- <args>]
npm restart restart module
Basic syntax
Copy the code as follows: npm restart [-- <args>]
In an article before the college entrance examination and Dragon Boat Festival, I wish the students in the college entrance examination to perform very well, and I wish you all a happy Zongzi Festival and spend more time with your family.
The above is the relevant information about the commonly used npm commands in Node.js introduced to you by the editor. I hope it will be helpful to everyone!