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

Node.js 101: Promise and async

先回顾一下 Sagase 的项目结构: lib/ cli.js sagase.js Gruntfile.js package.json 上一篇讲了 package.json,这一篇讲 lib/sagase.js。 因为代码比较长,就分开一节节地讲,完整的点开 GitHub 看吧。 'use strict'; 通知编译器进入 strict mode,主要的作用是让编译器帮你检查一些坑,有经验的 JavaScript 开发者可以忽略。 var fs =... Continue →