欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

全面指南:npm命令一览无余

最编程 2024-08-09 19:16:10
...

初始化package.json文件

npm init
// 想快速配置可以运行以下命令
npm init --yes

安装包(开发和生产都会用到)

npm install 包文件名
// 项目内安装包(package.json的dependencies中会自动添加该包文件名)

npm install -S 包文件名@版本号
// 指定版本的安装, -S 等价于 --save(--save是npm默认选项,可不加)

npm install 包文件名@latest
// 安装该包的最新版本

安装包(只在开发时用)

npm install 包文件名 -D
// -D 等价于 --save-dev

安装用的属性简写含义

-S, --save: Package will appear in your dependencies.
-D, --save-dev: Package will appear in your devDependencies.
-O, --save-optional: Package will appear in your optionalDependencies.

查看安装的包

// 当前安装的包
npm list --depth 0

 // 全局安装的包
npm list -g --depth 0

// -g: 全局的安装包 
// list:已安装的node包 
// –depth 0:这个参数我就不清楚了,可能是深度0,搜了一下没有找到解释

查看指定的包版本

npm list 包名

查看包的版本

npm view 包名 versions  // 查看所有版本
npm view 包名 version  // 查看最新版本

安装淘宝镜像

npm install -g cnpm --registry=https://registry.npm.taobao.org

卸载包

npm uninstall express

更新包

npm update express

搜索包

npm search express

npm, yarn查看源和换源

npm config get registry  // 查看npm当前镜像源
npm config set registry https://registry.npm.taobao.org/  // 设置npm镜像源为淘宝镜像
yarn config get registry  // 查看yarn当前镜像源
yarn config set registry https://registry.npm.taobao.org/  // 设置yarn镜像源为淘宝镜像

镜像源地址部分如下

npm --- https://registry.npmjs.org/
cnpm --- https://r.cnpmjs.org/
taobao --- https://registry.npm.taobao.org/
nj --- https://registry.nodejitsu.com/
rednpm --- https://registry.mirror.cqupt.edu.cn/
npmMirror --- https://skimdb.npmjs.com/registry/
deunpm --- http://registry.enpmjs.org/

常见错误及解决办法

\\ 错误
npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 
\\ 解决办法
npm config set proxy null
\\ 错误
npm resource busy or locked.....
\\ 解决办法
npm cache clean
npm install
\\ 错误
WARN deprecated circular-json@0.3.3: CircularJSON is in maintenance only, flatted is its success
\\ 解决方案
npm cache clean --force