npm之基本使用
# 查看镜像源 npm config get registry # 设置镜像源 # 腾讯云 npm config set registry http://mirrors.cloud.tencent.com/npm/ # 淘宝 npm config set registry https://registry.npmmirror.com # 华为云 npm config set registry https://mirrors.huaweicloud.com/repository/npm/
基本命令
1.初始化项目(生成package.json)
npm init
2.添加依赖
npm install(i) [package]@[version] (-S/-D) (-g) 参数解析: 1.install(i) install有一个简写i 2.[package]@[version] package为npm包名,后面的@[version]可选,没有的时候默认安装最新版本 3.-S/-D,-S是--save的简写,-D是--save-dev的简写,-S与-D的区别就是-S会被打包到运行环境中去,-D只是在开发中使用,比如babel转码 4.-g是-global的简写,这个选项会被npm包安装到全局去,一般用于一些在命令行使用的npm包,比如vue-cli,使用-g时没有-S/-D选项
3.删除依赖
npm uninstall (-g) [package]
4.更新依赖
npm update (-g) [package]
查看过期包 npm outdated [package]
查看包版本 npm ls [package]
5.运行脚本
npm run [scriptname]
常见的有vue-cli中的npm run dev、npm run build
6)查看镜像源
npm config get registry 设置镜像源: npm config set registry https://registry.npm.taobao.org 或者直接查看文件: .npmrc
7)设置缓存目录
npm config get cache npm cache clean --force npm config set cache "F:\ProgramFile\nodejs\node_module\node_cache" npm cache verify
nrm的使用
nrm:npm registry manager(npm仓库地址管理工具)
如果在国内开发的话,可以使用淘宝服务器提供的镜像
如果是在国外开发的话,可以使用国外的官方镜像源
因此,我们可以使用nrm来方便的切换镜像源
[安装]
npm i -g nrm // -g 就是--global的缩写 i是install的缩写
[使用]
// 查看当前镜像源
nrm ls // 带*表示当前正在使用的地址 ls就是list的缩写
// 切换镜像源地址
nrm use taobao // 切换到淘宝提供的镜像源 以后下载的包都是从淘宝镜像源服务器来下载
pnpm使用:
安装:
npm install pnpm
常见问题:
1.npm\cnpm.ps1,因为在此系统上禁止运行脚本
解决:
在尝试运行pnpm或npm命令时遇到安全错误,系统禁止了脚本的执行。解决方案是通过管理员权限打开Windows PowerShell,输入`set-ExecutionPolicy RemoteSigned`并确认更改执行策略
powershell执行策略:https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.4
常见问题:
1. npm ERR! cb()never called!
1.以管理员模式打开cmd清除你的npm缓存 : npm cache clean -f 2.清除完缓存后,安装最新版本的Node helper: npm install -g n 如果发生错误执行:npm install -g n --force
执行npm install 发生错误: npm ERR! cb.apply is not a function
解决:
win + r 打开运行,输入%appdata% 删除 npm 和 npm-cache 文件夹 执行npm cache clean --force命令 此时应该就可以了。如果还不行,就执行卸载Node.js重新安装。
2. No Xcode or CLT version detected!
解决:
Mac升级到High sierra 后,发现执行使用npm 或者yarn 的时候,不时会报gyp: No Xcode or CLT version detected! 的错误, 原因是缺了xcode 的CLI 工具, 只要执行下面的命令来安装就可以了 sudo xcode-select --install 再执行: $ xcode-select --install 出现错误: xcode-select: error: command line tools are already installed, use "Software Update" to install updates 解决: $ sudo rm -rf /Library/Developer/CommandLineTools $ sudo xcode-select --install
3. code for hash md5 was not found
The case for me is that when I install dependencies of a django web app, it messes up the environment. When I type cd, it shows the same error. The problem was the openssl library, it can not find the correct ones. If you are on Macintosh, you can type ls /usr/local/Cellar/openssl to see all the versions, brew switch openssl 1.0.XXXX to choose the available openssl version. Then the error is gone :)

浙公网安备 33010602011771号