今年开始就正式步入使用 mac os 系统的时代,使用了11年(2010-2020年)的 windows 系统,终于明白什么叫做转换成本。mac os 系统使用起来,的确十分流畅,没有广告,不用下载杀毒软件。作为开发人员,visual studio code 是必备。在使用过程中也遇到了一些问题,现将其一一记录下来,方便再遇到相同问题时,能够快速反应并解决掉。
问题1:nvm is not compatible with the npm config “prefix”…
现象:使用 mac os 系统后,发现每次打开 vscode,在命令栏 TERMINAL 中都会出现不识别 npm,把 vscode 重新安装后,还是不行。每次打开的时候,也会报下面的错误。
1 | nvm is not compatible with the npm config "prefix" option: |
原因: 我刚开始是使用brew install node
安装过 node
,而后来又用 nvm
安装过node
,而显示是使用nvm
安装的node
。只需要把之前的node
删除掉,就可以了。
解决办法:执行下面的命令,将通过 brew
命令安装的 node
& nvm
& npm
都删除掉,只通过 nvm
安装 node
和 npm
。如果 /usr/local/bin
或 /usr/local/lib
下没有 node
、 npm
、 nvm
则不用处理。
1 | rm -R /usr/local/lib/node_modules/npm |
问题2:Error: Cannot find module ‘xxx’
现象:控制台报错 Error: Cannot find module ‘xxx’(例如 Error: Cannot find module ‘nan’,Error: Cannot find module ‘webpack’)
原因:这种情况一般是由于 node_modules
中的版本号和 package-lock.json
中锁定的版本不一致导致的,默认是按照 package-lock.json
中的版本号安装和执行。
解决办法:到项目文件夹下,(可以先备份一下项目以防万一)删除node_modules
文件和package-lock.json
文件。注意不是package.json。最后在项目下通过 npm install
重新安装所依赖的包。
1 | rm -rf node_modules package-lock.json |
问题3:complete:13: command not found: compdef
现象: 在安装完 nvm
,并在 .bash_profile
添加完 nvm
的环境变量,如下:
1 | export NVM_DIR="$HOME/.nvm" |
但在 vscode 中使用 nvm 命令时,控制台会报 complete:13: command not found: compdef
原因:最后一行的命令会执行 compdef
,而该命令产生冲突,导致无法识别。
解决办法:十分简单,注释掉最后一行的代码即可。
问题4:GitLens was unable to find Git. Please make sure Git is installed.
现象:在 Mac 升级后,vscode 中发现 Git 居然不能用了,终端控制台 Git 无法使用的错误。
完整的错误信息:
1 | GitLens was unable to find Git. Please make sure Git is installed. Also ensure that Git is either in the PATH, or that 'git.path' is pointed to its installed location. |
看了一下Git命令是否能用 git --version
或 git --help
,发现还是报上面的错误。于是在终端控制台输入 which git
, 找到 git
的安装路径 /usr/bin/git
。
原因:Git 路径被重置为空了。此时需要找到 vscode 设置(左下角头像下面)里面,输入 git.path
,打开 setting.json
,修改成下面的样子。
1 | "window.restoreWindows": "preserve", |
修改后,再次输入 git --version
或 git --help
,还是会报错,错误如下:
1 | Error: Git must be installed and in your PATH! |
此时,在终端控制台,输入 xcode-select --install
,待 xcode 安装完毕重启 vscode,Git
便可以正常使用了。