问题背景:原mac电脑上已安装了node跟npm,但是在某次的误操作过程中发现打npm -v命令打不出来了,提示command not found。鼓捣了好多次,最后果断经过各种搜索卸载干净后再全部重新安装。
出现的问题:
1 node -v 能有版本提示,npm -v没有版本提示,且输出 npm command not found
2 brew install node 能有版本提示, brew install npm 提示出错。
解决方法:
(1) 删除旧文件:根据提示删除,或者直接删除(可以在访达中进行操作)
sudo npm uninstall npm -g
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*
sudo rm -rf /usr/local/include/node /Users/$USER/.npm
sudo rm /usr/local/bin/node
sudo rm /usr/local/share/man/man1/node.1
sudo rm /usr/local/lib/dtrace/node.d
确认是否卸载掉:
node -v
npm -v
(2)重新安装node
brew install node
(3)把node关联到软链接
brew link node
这时候我们可能会出现如下的结果:
Linking /usr/local/Cellar/node/11.2.0...
Error: Could not symlink include/node/common.gypi
Target /usr/local/include/node/common.gypi
already exists. You may want to remove it:
rm '/usr/local/include/node/common.gypi'
To force the link and overwrite all conflicting files:
brew link --overwrite node
To list all files that would be deleted:
brew link --overwrite --dry-run node
localhost:wkdir meng$ brew link --overwrite node
Linking /usr/local/Cellar/node/11.2.0...
Error: Could not symlink include/node/common.gypi
/usr/local/include/node is not writable.
(4)然后我们根据提示进行强制执行:
brew link --overwrite --dry-run node
强制执行后可能会需要我们删除一些之前有关node 的文件,类似下面
Would remove:
/usr/local/include/node/common.gypi
/usr/local/include/node/config.gypi
/usr/local/include/node/libplatform/libplatform-export.h
/usr/local/include/node/libplatform/libplatform.h
/usr/local/include/node/libplatform/v8-tracing.h
(5)继续删除上面的文件并再一次运行
brew link --overwrite node
安装npm
sudo npm install --registry=https://registry.npm.taobao.org
再次检查node、npm
node -v
npm -v
成果:
注意:出现问题的情况可能各种各样,当出现一个问题的时候要试着baidu,然后再继续解决下一个问题。
更多个人教程请访问个人主页:
Github搭建个人博客(2019最新版,亲测)
.https://blog.csdn.net/xudailong_blog/article/details/78762262
公众号: