Use io.js and Node.js with Homebrew at OS X

Install [io.js] and [Node.js] with [homebrew] is super easy:

brew install iojs node

But you make notice this message for io.js formula:

This formula is keg-only.
iojs conflicts with node (which is currently more established)

Because of io.js uses node for its binary’s name, so Homebrew won’t link io.js’s binary to /usr/local/bin, unless force to link with command brew link iojs --force. That means you can’t use io.js directly (e.g. node app.js). You have to use absolute path (e.g. /usr/local/Cellar/iojs/1.0.4/bin/node app.js), or use nvm:

nvm use 0.11
node -v # v0.11.15
nvm use iojs
node -v # v1.0.4

Although it can work, but you have to change asolute path after io.js updated, or nvm may be unavailable in some situations (e.g. needs source $NVM_PATH/nvm.sh). It’s inconvenience. So I created a shell script to help things easier:

#!/usr/bin/env sh

if [ -d "$(brew --prefix)/Cellar" ]; then
  node=`find /usr/local/Cellar -name node | grep iojs/.*/bin/node`
  if [ ! -z "$node" ]; then
    $node $@
  else
    echo "io.js not installed"
  fi
else
  echo "Homebrew folder not found"
fi

This script searches io.js’s binary in Homebrew’s Cellar folder, and run it with passed arguments. You can consider it is an alias of /usr/local/Cellar/iojs/1.0.4/bin/node, except it will maintain version for you.

All you need to do is put this script into your $PATH and make it executable. If you don’t know how to, here is a guide:

cd $HOME
touch .bash_profile # or .bashsrc, .zshrc, depends on your environment
echo 'export PATH="$HOME/.scripts:$PATH"' >> .bash_profile
mkdir .script && cd $_
touch iojs && chmod +x iojs
open -a "EDITOR" iojs

Replace EDITOR to your favorite editor, then copy and paste the shell script into the file, and save.

That’s all. You can now run io.js and Node.js like this:

node -v # v0.11.15
iojs -v # v1.0.4

node app_for_node.js

iojs app_for_iojs.js

Note that this script can’t handle multiple versions for now, so after io.js updated, remember to run brew cleanup.

The shell script is published on Gist.

[io.js]: https://iojs.org/
[Node.js]: http://nodejs.org/
[Homebrew]: http://brew.sh/

 
26
Kudos
 
26
Kudos

Now read this

Markdown and CommonMark

CommonMark,最早的名字叫 Standard Markdown,后来迫于 Markdown 原作者 John Gruber 的压力而改名。 虽然这已经是今年九月初的事情了,而且我在当时就已经表达了我的看法,但还是完整说说我的观点。 我对的 Markdown 的看法是初始版本加上 GitHub Flavored Markdown 的代码块语法即可,其他的,算了吧,何必这么复杂?— Chris (@chrisyipw) September 4, 2014... Continue →