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

Control Flow in JavaScript

这是一篇我在飞飞商城针对 JavaScript Control Flow 的演讲稿文字版。 Control Flow 是什么? # Control Flow 就是控制流,简单地归纳,就是代码执行的顺序(和控制的手段)。 常见的控制流有: if-else switch-case for / while loop goto 问题儿:JavaScript # 由于 JavaScript 的运行环境,决定了她拥有异步执行的能力。 if (checkEmail() ===... Continue →