12 Apr
2011

Taking Baby Steps with Node.js – Some Node.js Goodies

Category:UncategorizedTag: :

Here are the links to the previous installments:

  1. Introduction
  2. Threads vs. Events
  3. Using Non-Standard Modules
  4. Debugging with node-inspector
  5. CommonJS and Creating Custom Modules
  6. Node Version Management with n
  7. Implementing Events
  8. BDD Style Unit Tests with Jasmine-Node Sprinkled With Some Should
  9. ?node_modules? Folders
  10.   Pumping Data Between Streams

For this post I want to quickly share a couple of goodies that can really improve the overall developer experience when building Node.js applications.

The first one that we?re going to discuss is nodemon. When we start a server process (e.g. $ node server.js), this process isn?t automatically restarted when we make changes to the source files of our node.js application. In order to load these new changes, we first need to manually stop the process and restart it again, which typically involves switching to the command-line before we can actually see the effects of our changes. This is were nodemon steps in, which monitors our Node.js application for changes and automatically restarts the process. This can be a real time saver because we can directly see the effects of our changes without going through the whole stop/restart ceremony.

Installing nodemon is as simple as:

npm install ?g nodemon

So instead of using:

node server.js

to start our application, we can now simply use:

nodemon server.js

That?s it! If we make a change to one of the JavaScript source files, nodemon automatically restarts the process.

[nodemon] v0.2.2
[nodemon] running server.js
[nodemon] starting node
[nodemon] reading ignore list

[nodemon] restarting due to changes…
[nodemon] ./server.js

nodemon also provides the ability to ignore some specific files, directories or file patterns using an ignore file (nodemon-ignore) that is automatically created in the root directory of the source code the first time the application is started.

# My ignore file

/images/*     # ignore all image files
*.css             # ignore all CSS files

Say that for some odd reason our Node.js applications crashes while handling a request (sounds quite silly, I know, but just hear me out). In this case, nodemon just shows the regular error output and waits until one of the source files is changed again before restarting the application.

[nodemon] app crashed – waiting for file change before starting…

 

The second life-saver that I want to mention here is a library named long-stack-traces. When we?re getting a runtime error in our  application, Node.js generally provides us some very brief error message and stack trace.

ReferenceError: test is not defined
    at Server.<anonymous> (/cygdrive/c/server.js:9:17)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1102:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1001:22)
    at Socket._onReadable (net.js:675:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

Fortunately, long-stack-traces`shows us a more extended stack trace which enables us to find out more quickly which particular line of code that caused the error.

Uncaught ReferenceError: test is not defined
    at Server.<anonymous> (/cygdrive/c/server.js:9:17)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1102:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1001:22)
    at Socket._onReadable (net.js:675:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)
—————————————-
    at EventEmitter.addListener
    at new Server (http.js:947:10)
    at Object.createServer (http.js:964:10)
    at Object.<anonymous> (/cygdrive/c/server.js:4:6)
    at Module._compile (module.js:404:26)
    at Object..js (module.js:410:10)
    at Module.load (module.js:336:31)
    at Function._load (module.js:297:12)
    at Array.<anonymous> (module.js:423:10)

All you need to do is install the library into the node_modules directory for your application:

npm install long-stack-traces

and simply add:

require(‘long-stack-traces’);

to your code.

There you go. I hope these two goodies can make your Node.js development experience even more pleasant. Until next time.

One thought on “Taking Baby Steps with Node.js – Some Node.js Goodies”

Comments are closed.