Modifying $NODE_PATH for Node.js/NPM/NVM

I've been hacking around with Node.js lately. If you develop with Node.js then you should be using both NPM (package manager) and NVM (node version manager, like RVM).

For some reason, NVM doesn't seem to set \$NODE_PATH when you switch Node versions. \$NODE_PATH needs to be set so that when you call 'require' from within your Node programs, it can find the relevant modules.

If you don't set \$NODE_PATH, that's OK too. But you'll find that in your project directory you'll need to do a lot of linking. You can do this like so:

npm install -g express #the '-g' flag installs the module to the npm node_module directory
npm link express

Doing this is OK, but is a bit annoying at times because some modules still won't work if you link with them, so you're left installing it locally to your project's directory.

I hacked up some bash commands to set my Node environment how I like it. This also allows me to make my own Node modules.

My ~/.bash_profile on Mac OS X Lion

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
PROJ=~/Dropbox/Projects
. ~/.nvm/nvm.sh

JP_NODE_PATH=/Users/jprichardson/Dropbox/Projects/Personal/js/node_modules
JP_NODE_BIN_PATH="${JP_NODE_PATH}/.bin"

NP=$(which node) 
BP=${NP%bin/node} #this replaces the string '/bin/node'
LP="${BP}lib/node_modules"

export PATH="$PATH:$JP_NODE_BIN_PATH"
export NODE_PATH="$JP_NODE_PATH:$LP"

When I run `which node` this is capturing the output from my default Node version. You set this like: nvm alias default 0.4 which will use the latest 0.4, which is 0.4.12.

Again, I'm not a bash hacker by any means. I didn't even know why you need to use 'export'. I didn't know how to concatenate strings in a bash variable. Or, even how to run a command and capture the output in a variable. I also didn't know how to modify strings in bash. What kind of hacker am I? ::sobs:: Hehehe.

If you made it this far, you should follow me on Twitter.

-JP

Want to test-drive Bitcoin without any risk? Check out my bitcoin wallet Coinbolt. It includes test coins for free.

comments powered by Disqus