npm
(Node Package Manager)是Node.js生态系统中不可或缺的部分,它负责软件包的管理,包括安装、更新、卸载以及版本控制等功能。下面是一些常用的npm
命令及其实际应用场景的详解:
npm init
或 npm init -y
package.json
文件,用于存储项目的元数据和依赖关系。使用-y
参数会接受所有默认值快速初始化。npm install
或 npm i
@
,如npm install express@4.17.1
。-S
或 --save
:将包添加到dependencies
列表,用于生产环境。-D
或 --save-dev
:将包添加到devDependencies
列表,用于开发环境。-E
或 --save-exact
:精确保存包的确切版本号。npm uninstall
package.json
中删除相关条目。npm update [package]
npm list
或 npm ls
npm run
package.json
中的脚本。例如,npm run start
通常用于启动开发服务器。npm view [property]
npm publish
npm install -g
npm uninstall -g
create-react-app
。npm cache clean --force
npm version
package.json
中自动递增版本号。patch
小版本,minor
中版本,major
大版本。npm search
npm link
npm link
,然后在使用该模块的项目中执行npm link
package.json
中定义脚本时,可以使用"scripts": {"alias": "command-to-run"}
"start": "node server.js"
。npm config list
npm config set registry https://registry.npmjs.org/
https://registry.npm.taobao.org/
以加速下载。npm install --save-dev
devDependencies
中,适用于那些只在开发和构建过程中需要的包。npm outdated
npm install
或npm ci
(如果你有package-lock.json
或npm-shrinkwrap.json
文件)。
package.json
或锁定文件安装所有必要的依赖,非常适合CI/CD流程或团队协作时确保环境一致性。npm help
或 npm --help
npx [args]