Node.js Tips
Here are some useful notes regarding Node.js development.
npm --save
When I first learned how to use npm, the process was like this:
npm install <package>
vi package.json # edit the dependencies manually, and have the package version be '*'
which was a huge pain. Turns out, there is a great command-line flag which will add the module to package.json automatically.
npm install <package> --save # save to package.json with version
npm install <package> --save-dev # save to dev dependencies
npm local install
Installing dependencies globally (-g) can be quite scary because by default you have to sudo the command. In order to bypass this, we can compile npm to install locally to our home folder, and then add that folder to our path (.local directory). (source)
wget http://nodejs.org/dist/v0.10.12/node-v0.10.12.tar.gz
tar zxvf node-v0.10.12.tar.gz
cd node-v0.10.12
./configure --prefix=~/.local
make
make install
export PATH=$HOME/.local/bin:$PATH
npm publish
Publishing a module on npm couldn't be easier (take from this gist):
npm set init.author.name "Your Name"
npm set init.author.email "[email protected]"
npm set init.author.url "http://yourblog.com"
npm adduser
cd /path/to/your-project
npm init
npm publish .
--expose-gc
The V8 javascript garbage collector in node.js is usually pretty good, however there may be some times when you need fine control over the collection yourself. In those cases, this command is quite useful:
node --expose-gc app.js
global.gc(); # within app.js
npm link
Sometimes I find myself needing to modify an npm module, either to fix a bug or add a feature. In order to test my local modifications, and use my version across apps easily, I can use npm link
:
git clone [email protected]:Zolmeister/Polish.js.git
cd Polish.js
npm link
cd ~/path/to/app
npm link polish # instead of npm install polish
Bonus - Great modules:
socket.io - realtime websockets magic
request - making http requests easier (like the python library)
passport - user authentication
Q - great promise library
async - if you're not cool enough for promises
lodash - better than underscore
fs-extra - lets you actually copy/paste/rm -rf files/folders properly
mongojs - great library for working with mongodb
nodejitsu reccomendations