天天看點

Mac安裝Homebrew,node,npm

前言

最近開始正式使用TypeScript。考慮用typedoc這個TypeScript 文檔化工具。結果家裡的新Mac居然npm都沒有.

首先,npm是個啥?

npm在Node v0.6.x版本之後,内建于Node系統。通過npm可以協助開發者安裝、解除安裝、删除、更新Node.件,并且可以通過npm釋出自己的插件。那麼就好辦了,先安裝Node後就自帶npm了。

Node.js 是一個基于 Chrome V8 引擎的 JavaScript 運作環境。

Node.js 使用了一個事件驅動、非阻塞式 I/O 的模型,使其輕量又高效。摘自:nodejs.cn

于是乎。。

MacOS安裝xcode command line tool的兩種方法

參考:百度經驗-如何安裝command line tools
  • 打開終端輸入:

    xcode-select --install

    然後點選安裝
  • 登入https://developer.apple.com/download/more/ 然後下載下傳dmg安裝

Homebrew安裝

Homebrew簡稱brew,是Mac OSX上的軟體包管理工具,能在Mac中友善的安裝軟體或者解除安裝軟體。

打開終端,執行以下指令安裝Homebrew,確定xcode command line tool已經安裝,本文開頭有安裝步驟。

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
           

執行上面指令後會提示輸入系統密碼,輸入密碼繼續安裝。

l2xindeiMac:~ l2xin$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew

Press RETURN to continue or any other key to abort
==> Downloading and installing Homebrew...
remote: Enumerating objects: 127, done.
remote: Counting objects: 100% (127/127), done.
remote: Compressing objects: 100% (70/70), done.
remote: Total 113510 (delta 79), reused 83 (delta 57), pack-reused 113383
Receiving objects: 100% (113510/113510), 26.17 MiB | 23.00 KiB/s, done.
Resolving deltas: 100% (83016/83016), done.
From https://github.com/Homebrew/brew
 * [new branch]          master     -> origin/master
 * [new tag]             1.7.7      -> 1.7.7
 * [new tag]             1.8.0      -> 1.8.0
HEAD is now at 18bac4fa5 Merge pull request #5204 from GauthamGoli/immutable-args
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Tapping homebrew/core
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...

remote: Enumerating objects: 4858, done.
remote: Counting objects: 100% (4858/4858), done.
remote: Compressing objects: 100% (4654/4654), done.
remote: Total 4858 (delta 55), reused 327 (delta 14), pack-reused 0
Receiving objects: 100% (4858/4858), 4.04 MiB | 49.00 KiB/s, done.
Resolving deltas: 100% (55/55), done.
Tapped 2 commands and 4644 formulae (4,900 files, 12.6MB).
==> Migrating /Library/Caches/Homebrew to /Users/l2xin/Library/Caches/Homebrew..
==> Deleting /Library/Caches/Homebrew...
Already up-to-date.
==> Installation successful!

==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Next steps:
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh
           

如安裝成功則會看到上面:

Installation successful!

安裝Node

brew install node
    執行以下指令檢視是否安裝成功
    node -v:檢視node版本
    npm -v:檢視npm版本
           

有人遇到問題 mac上用brew把node裝好了,卻沒有npm,很不幸,我也遇到了,怎麼辦?

建議node不用brew裝 解除安裝node,安裝包安裝

brew uninstall node
           

官方下載下傳

https://nodejs.org/dist/v11.0.0/node-v11.0.0.pkg

taobao鏡像

https://npm.taobao.org/mirrors/node

l2xindeiMac:~ l2xin$ npm -v
6.4.1
l2xindeiMac:~ l2xin$ node -v
v11.0.0
l2xindeiMac:~ l2xin$ 
           

搞定了。。

更新node

這是後話,以後需要再試。

https://blog.csdn.net/u012982629/article/details/80526385

解決國内NPM(node.js)安裝依賴速度慢問題

這個我倒是沒驗證,搜到的:

https://blog.csdn.net/nnsword/article/details/54096268

不知道各位是否遇到這種情況,使用NPM(Node.js包管理工具)安裝依賴時速度特别慢,後來在網上找了好久才找到一種最佳解決辦法,在安裝時可以手動指定從哪個鏡像伺服器擷取資源,我們可以使用阿裡巴巴在國内的鏡像伺服器,指令如下:

npm install -gd webpack --registry=http://registry.npm.taobao.org
           

隻需要使用–registry參數指定鏡像伺服器位址,為了避免每次安裝都需要–registry參數,可以使用如下指令進行永久設定:

npm config set registry http://registry.npm.taobao.org
           

後記

全部搞好居然已經很晚了,安裝下載下傳太慢。。有空試試改鏡像位址,再接着搞typedoc

參考

  • https://nodejs.org/en/download/
  • https://blog.csdn.net/moyummy/article/details/54317866
  • https://blog.csdn.net/zfangls/article/details/55098299
  • https://segmentfault.com/q/1010000006447817/a-1020000006449033
  • https://blog.csdn.net/nnsword/article/details/54096268