終於解決node和npm問題:Unknown command ‘node’

Pei Lee

如題,我照著網路上的node.js和npm教學安裝步驟,結果卻發生一連串問題,故此寫下一些記錄,希望之後碰到的人都能順利解決。(只有在Mac OS上有遇到,在Windows卻沒遇到,不知道為什麼,請求大神解惑)

方法一:Mac 重新安裝 npm,把資料清乾淨

$ brew uninstall — ignore-dependencies node 
$ rm -rf /usr/local/bin/node
$ rm -rf /usr/local/bin/npm
$ rm -rf /usr/local/bin/node_modules

重新安裝


$ brew install node
$ npm
-v
env: node: No such file or directory

結果,失敗。


方法二:sudo chown -R $(whoami) $(brew — prefix)/*

因為我的commend是安裝fish shell所以要拿掉(whoami)前面的$字號

$ brew install node      ...
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall node`
...
$ sudo chown -R (whoami) $(brew --prefix)/*
Password:

輸入完密碼,再次測試 node -v

$ node -v
Unknown command ‘node’
$ brew install node
Warning: node 12.1.0 is already installed, it's just not linked
You can use `brew link node` to link this version
$ brew link node
Linking /usr/local/Cellar/node/12.1.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

根據建議執行 brew link — overwrite node

$ brew link — overwrite nodeLinking /usr/local/Cellar/node/12.1.0…
Error: Could not symlink include/node/common.gypi
/usr/local/include/node is not writable.

權限不足無法覆寫,改檔案權限再試一次

$ sudo rm ‘/usr/local/include/node’
$ sudo chown -R $USER /usr/local
$ brew link --overwrite node
$ npm

然後就成功了!!

看看node 和 npm 版本

$ npm -v
6.9.0
$ node -v
v12.1.0

哭出來感動,終於解決!!

目前就先這樣啦~

參考來源:感謝網路上的各位前輩們

https://khiav223577.github.io/blog/2017/09/24/%E4%BF%AE%E5%BE%A9-npm-%E7%9A%84%E6%BC%AB%E6%BC%AB%E9%95%B7%E8%B7%AF/

https://teamtreehouse.com/community/problem-installing-node-using-homebrew

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade