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

Function direct call vs func.call() vs func.bind() in JavaScript

JavaScript 有三种函数调用方式 (其他奇葩的不讨论) : func() func.call() 和 func.apply() var binded = func.bind() 第一种很好理解,就是直接调用。 第二种很常用,经常用于调整函数的 this 指向的对象。如: function say_hello () { return 'Hello, ' + this.name } say_hello.call({ name: 'John' }) //... Continue →