Skip to content

Instantly share code, notes, and snippets.

@mikaelhm
Created December 2, 2013 21:44
Show Gist options
  • Select an option

  • Save mikaelhm/7759637 to your computer and use it in GitHub Desktop.

Select an option

Save mikaelhm/7759637 to your computer and use it in GitHub Desktop.
Node.js Development without Eclipse
Node.js development without Eclipse
===================================
Currently, many Node.js developers use Eclipse with the Nodeclipse as an IDE for developing node.js applications. However, as some think that eclipse can be too bloated and it does not support all features for node.js, e.g. proper code-analysis/intelliSense does not work for javascript. Furthermore, alternatives are always good, here is a description of a development environment for node.js.
Content
-------
- Editor (for hacking javascript)
- Node Monitor (auto-restarting node.js on code changes)
- Debugging
- Node Inspector (debugger)
- Editor
---------
This description does NOT rely on a particular editor. It lets the developer choose his/her own prefereable editor. I suggest new developers try out Sublime Text 3 Beta, as it is a very powerfull modern editor with support for very helpful plug-ins that can make javascript development even easier. You can download it here: http://www.sublimetext.com/3
**Suggested Sublime Text Packages**
- `Package Installer` -- Lets you install new packages from repository, very easily!
- `Sidebar Enhancements` -- Better context menu in the filesidebar.
- `SublimeLinter` -- On the fly linter'er with notification in left-margin(next to line-numbers).
- `Auto Semi Colon` -- Moves semi-colon outside of the last bracket when pressed inside one or more pairs of brackets.
- `SublimeCodeIntel` --
- `Git`
- `FileDiffs`
- `Pretty JSON`
**Suggested Sublime Test Settings**
The following can be added to your user settings via the menu:
`Preferences > Settings - User `
- `"trim_trailing_white_space_on_save": true` -- this will automatically trim trailing white spaces when you save a file.
- `"indent_guide_options":
[
"draw_normal",
"draw_active"
],` -- This will hillight you indent guidelines in a very helpfull way.
The settings file is a JSON file, mine looks like this:
```JSON
{
"ignored_packages":
[
"Vintage"
],
"indent_guide_options":
[
"draw_normal",
"draw_active"
],
"trim_trailing_white_space_on_save": true
}
```
Node Monitor (`nodemon`)
------------------------
To run a node.js application we noramlly start it by running the command
`$ node server.js`
If we do any changes the code we need to restart the node server. When developing this can be a tedious task. Thanks to [Remy Sharp][1] and his [nodemon][2] application this is no longer needed. Nodemon will monitor the files from the root folder of the application and all its subfolders, and restart the server on any file change.
Nodemon is installed easily with `npm` like this:
`$ npm install -g nodemon` (-g for globally)
**Running a node.js appication with nodemon**
It is very easy to run you application with nodemon:
`$ nodemon server.js`
*Note: You might need to add it to you environment variable PATH, on windows see [here][3].*
Debugging
---------
The V8 enginge of node.js has a built in debuging mode. This mode can be started when stating node by adding the `--debug` flag when starting. i.e.
`$ node --debug server.js`
Node will then open a socket on port 5858 (default port), which you can hook a debugger onto.
In case you want to debug something in the initialization phase of you node application you can use the flag `--debug-brk` instead, i.e.
`$ node --debug-brk server.js`
This will break your node application on the first line.
Nodemon also supports these flags so you can run:
`$ nodemon --debug server.js` or `$ nodemon --debug-brk server.js`
Node Inspector (`node-inspector`)
---------------------------------
Node Inspector is a debugger interface for node.js using the Blink Developer Tools (former WebKit Web Inspector). It hooks into your node.js application running in debug mode.
The project was started by [Danny Coates][4] and now maintaind by [StrongLoop][5].
It can also be installed via `npm` by
`$ npm install -g node-inspector` (-g for globally)
**Starting Node Inspector**
Node Inspector can be started from anywhere by: (it does not look directly at your files)
`$ node-inspector`
This will start node inspector on port `8080` and you can access the node-inspector by accessing the following url in you chrome browser:
`http://localhost:8080/debug?port=5858`
This will give you the debugger UI, where you can set breakpoint, add stuff to watch and look at your call stack.
the parameter `port=5858` tells node-inspector to connect to the V8 engine on port 5858(default debug port for node.js).
In case your application is using port `8080` you can start node-inspector with the flag `--web-port=8282` to start it on port `8282`, i.e
`$ node-inspector --web-port=8282`
**Tips and Trick, when using Node Inspector**
- Add to watch -- anywhere in your code you can select a variable or expression and add it to watches by right clicking it.
- Using the console -- You show the javascript console by clicking the button in the lower right corner. The console can be very helpful as you can use it to evaluate expression in the given environment of a break point. E.g you can access variables
Summary
-------
`node-inspector --web-port=8282` Start node inspector on port 8282
`nodemon --debug server.js` Start nodemonitor in debug mode
OR:
`nodemon --debug-brk server.js` Start nodemonitor in debug mode and break on first line
Open this url in Chrome http://localhost:8282/debug?port=5858
[1]: https://github.com/remy
[2]: https://github.com/remy/nodemon
[3]: http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/5445775168.htm
[4]: https://github.com/dannycoates
[5]: http://strongloop.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment