Skip to content

Instantly share code, notes, and snippets.

@wastemobile
Last active December 30, 2015 22:09
Show Gist options
  • Save wastemobile/7892305 to your computer and use it in GitHub Desktop.
Save wastemobile/7892305 to your computer and use it in GitHub Desktop.
Install Node。在 Mac 上使用 homebrew 安裝 node 雖然方便,但後續更新與使用 npm 安裝與升級套件時會遇到問題,實際原因是來自于目錄權限。要不是使用時必須帶著 sudo,要不就是得修改部分目錄權限,兩者都不太好。下面的安裝程序可以避掉。

說明事項:

在 Mac 上使用 homebrew 安裝 node 雖然方便,但後續更新與使用 npm 安裝與升級套件時會遇到問題,實際原因是來自于目錄權限。要不是使用時必須帶著 sudo,要不就是得修改部分目錄權限,兩者都不太好。下面的安裝程序可以避掉。

npm install -g n 是安裝npm的node版本管理套件,使用時只需要 n stable 或指定版本即可。(預設安裝完就是目前的穩定版本。)

bower 與 grunt-cli 是個人選擇,不需要的可以註解掉。

Mac 上設定個人路徑通常是用個人根目錄下的 .bash_profile 即可,加入前面三行才能讓 .bashrc 檔案中的設定生效,知道怎麼調整這些的 Linux 高手也可自行處理。後面的命令則是讓 Mac 終端機在 Git 目錄下顯示當前分支、距離上次提交的時間等額外選項。(直接修改 .bash_profile 加入路徑即可。)

if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
function git_branch {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return;
echo "("${ref#refs/heads/}") ";
}
function git_since_last_commit {
now=`date +%s`;
last_commit=$(git log --pretty=format:%at -1 2> /dev/null) || return;
seconds_since_last_commit=$((now-last_commit));
minutes_since_last_commit=$((seconds_since_last_commit/60));
hours_since_last_commit=$((minutes_since_last_commit/60));
minutes_since_last_commit=$((minutes_since_last_commit%60));
echo "${hours_since_last_commit}h${minutes_since_last_commit}m ";
}
PS1="[\[\033[1;32m\]\w\[\033[0m\]] \[\033[0m\]\[\033[1;36m\]\$(git_branch)\[\033[0;33m\]\$(git_since_last_commit)\[\033[0m\]$ "
[ -f ~/.git-bash-completion.sh ] && . ~/.git-bash-completion.sh
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install
node -v
npm -v
npm install -g n
npm install -g bower
npm install -g grunt-cli
完全的鬼打牆,不管怎麼更新 npm 都碰到問題,檢查一下發現 npm 居然無法更新自己,查了一堆網路上的資訊,發現只要使用 homebrew 安裝 node,幾乎都會遇到[這個問題](https://github.com/Homebrew/homebrew/issues/22408),而且沒啥好的解法。多半是乾脆自己建立 symlink,但實在不太漂亮。
最終還是確認,使用一個簡易的套件安裝管理程序(homebrew),若是碰上另一個套件管理程序(例如 gem、npm),泰半下場不太好...XD
既然 Ruby 已經改用 rbenv 管理,就乾脆狠下心移除掉自行安裝、透過 homebrew 快速安裝 Node.js 的捷徑,改安裝 nvm 進行。
```
brew install nvm
```
但 link 也總是出錯,最後手動建立了...
```
ln -sf /usr/local/Cellar/nvm/0.2.0/nvm.sh /usr/local/bin/nvm
```
再到 `.bash_profile` 添加兩行:
```
source nvm
nvm alias default v0.10.26
```
目前還算 OK,但感覺不太紮實,不知哪時又會出錯...XD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment