转载请注明出处,谢谢
常用npm命令
| 命令 | 作用 | 说明 |
|---|---|---|
| npm -v | 查看电脑所安装npm包的版本号 | 注意空格 |
| npm i | 自动检索下载依赖包 | \ |
| npm init | 初始化 | \ |
| npm init -y | 初始化 | -y 以默认值的方式初始化(自动按下回车键) |
| npm i 包名 | 安装包 | \ |
| npm i 包名@版本号 | 安装指定版本的包 | \ |
| npm uninstall 包名 | 删除包 | \ |
| npm ls | 查看本地模块 | \ |
| npm ls -g | 查看全局模块 | \ |
| npm update 模块包名 | 更新模块 | \ |
| npm install npm -g | 更新npm模块 | \ |
| npm search 模块包名 | 搜索模块 | \ |
| npm publish 模块包名 | 发布模块 | \ |
重点强调: 如果项目中 有package-lock.json package.json 而没有 node_modules 执行命令 npm i 系统就会自动检索 package.json 里面记录依赖包 然后重新下载
安装常用包(三方):
| 命令 | 作用 |
|---|---|
| npm i express | 服务器相关 |
| npm i -g nodemon | 自动重启服务器 (nodemon终端命令 -g 全局使用) |
| npm i mysql | 数据库模块 |
| npm i cors | 跨域请求模块 |
| npn i uuid | 随机id |
常用命令后缀:
npm i 模块包名 -g: 全局安装 将模块包安装到node根目录下 以后这台电脑所有的项目都可以直接使用这个模块
npm i 包名 --sava: 会添加到package.json文件的dependencies里面
npm i 模块包名 --sava-dev: 会添加到package.json文件的devDependencies里面
注
dependencies: 放一些基础包 uuid jquery 运行时的依赖包 发布之后生产环境还要用的
devDependencies eslint(规范代码): 只有在开发的时候使用的
解决包下载慢问题(切换淘宝镜像):
方法一
更改npm配置文件:
npm config set registry URL
URL 即为需要设置的镜像站点地址,如淘宝镜像: https://registry.npmmirror.com
方法二:
npm i nrm -g 将nrm安装为全局可用工具
nrm ls 查看所有可用镜像源
nrm use taobao 将下包路径切换为taobao 镜像(同理,也可以切换其他国内镜像)
方法三:
npm config set registry https://registry.npmmirror.com,切换国内镜像,使用淘宝镜像
npm config set registry https://registry.npm.taobao.org,切换国内镜像,使用淘宝镜像(该镜像过期)
npm config get registry,查看当前镜像使用的地址,返回成功,则代表设置成功
转载请注明出处,谢谢