Setting up IDE for Node.js

Being a .Net developer I am used to using features like intellisense, debugging tools and integrated test runner. While there are extensions in Visual Studio to enhance these features, the default setup is quite rich and works out of the box.

However, it is not the same for Node.js environment. After trying out a few editors, I choose to use WebStorm from JetBrains. In this post, I will explain how to configure WebStorm for the following features:

  • Enable intellisense for external libraries (eg. ExpressJS)
  • Use code snippets
  • Auto run unit tests as code changes

Enable intellisense for ExpressJS

While WebStorm provides a decent intellisense support for the core libraries and code within the project, it doesn’t do the same for third party libraries by default.

To enable intellisense for a third party library, go to Preference > JavaScript > Libraries.

Since ExpressJS is not available on the list, we need to download it.

WebStorm officially support only few libraries. However, if you choose the TypeScript community stubs option, you will find a long list of libraries. From here we can download & install intellisense support for ExpressJS.

Code Snippets

One of my favourite feature in Visual Studio IDE was code snippets. Luckily WebStorm also provides the option to create new snippets.

To create a new code snippet, highlight the text and go to Tools > Save As Live Template... This will bring up the following window:

First, choose the shortcut for your snippet in the Abbreviation field. Next, replace the words with variables that you want to set dynamically. That’s it, we have our own code snippet.

Configure Test Runner

There are many test frameworks available in javascript. I find QUnitJasmine and Mocha are getting popular these days.

Here I will explain how we can configure WebStorm to run jasmine tests. Then, we will enable it to automatically run the tests when we make changes in source code.

Lets create a simple calculator project with the following folder structure:

Now, setting up karma to run tests is really simple. We need to create a config file to specify some details. The easiest way to do that is from the terminal.

Simply run the karma init command from the terminal and provide the inputs. It will generate the karma.conf.js file.

For this demo project, the inputs look like this:

Please note that if you are running this for the first time, you may need to install karma-cli. You can do that using npm install -g karma-cli

To run the tests, right click on the karma.conf.js file and select the Run option. This will run the tests and open up the test results window.

Now, we can also configure it to run the tests when we change the source code. To enable it, click the auto-run button below the Run button. The auto test delay can also be adjusted from the settings.

I hope this helps you configure your Node.js development environment. As I learn more, I will share my experience on managing the codebase in the coming days.