Common NPM commands
NPM (Node Package Manager) is the package management tool for Node.js, used to install, manage, and share JavaScript code. Below are some commonly used NPM commands and their usage.
Initialize a Project
Use the npm init
command to create a new package.json
file, which describes the project information and dependencies.
npm init
After running, you will be prompted to enter details like the project name, version, description, etc. To quickly generate a default configuration, add the -y
flag:
npm init -y
Install Dependencies
Install Production Dependencies
Production dependencies are packages required for the project to run. Use the --save
or -S
flag (default behavior in NPM 5+):
npm install lodash
Equivalent to:
npm install lodash --save
Install Development Dependencies
Development dependencies are only needed during development, such as testing tools or build tools. Use the --save-dev
or -D
flag:
npm install jest --save-dev
Global Installation
Globally installed packages can be used anywhere, typically for command-line tools like nodemon
:
npm install nodemon -g
Install a Specific Version
You can specify a package version or version range:
npm install express@4.17.1
Or use semantic versioning:
npm install express@"^4.17.0"
Uninstall Dependencies
Use the uninstall
or un
command to remove a package:
npm uninstall lodash
To also remove the dependency from package.json
, add --save
or --save-dev
:
npm uninstall lodash --save
npm uninstall jest --save-dev
Update Dependencies
Update a Single Package
Use the update
command to update a package to the latest version:
npm update lodash
Update All Packages
Update all dependency packages:
npm update
Check for Outdated Packages
Use the outdated
command to see which packages need updating:
npm outdated
Run Scripts
Script commands can be defined in package.json
and executed with npm run
:
{
"scripts": {
"start": "node app.js",
"test": "jest"
}
}
Run a script:
npm run test
start
and test
are special scripts and can omit run
:
npm test
View Package Information
View Installed Packages
List all installed dependencies for the project:
npm list
Add the --depth
flag to limit the display hierarchy:
npm list --depth=0
View Package Details
Use the view
or info
command to view detailed information about a package:
npm view express
Publish a Package
Log in to NPM
First, log in to your NPM account:
npm login
Publish the Package
Run the following in the project directory:
npm publish
Update the Version Number
Before publishing a new version, update the version number using the version
command:
npm version patch # Minor version update
npm version minor # Medium version update
npm version major # Major version update
Clear the Cache
NPM caches downloaded packages. To clear the cache, use:
npm cache clean --force
Configure NPM
View Configuration
View the current NPM configuration:
npm config list
Set a Mirror Source
Switch to the Taobao mirror source:
npm config set registry https://registry.npmmirror.com
Revert to the default source:
npm config set registry https://registry.npmjs.org
Other Common Commands
Check Package Security
Use the audit
command to check the security of project dependencies:
npm audit
Fix security issues:
npm audit fix
Execute npx Commands
npx
is used to temporarily install and run packages:
npx create-react-app my-app
View Help
View help information for NPM commands:
npm help
Or view help for a specific command:
npm help install
本站部分内容来自互联网,一切版权均归源网站或源作者所有。
如果侵犯了你的权益请来信告知我们删除。邮箱:cc@cccx.cn
上一篇:package.json文件详解
下一篇:语义化版本控制