Context:

Here at IntelligentBee we wanted to establish a few ground rules that all of our developers should follow when developing an app in Node.js. In that way all of us would be familiar with a particular set of tools, something that would make us deliver better software, fast. Here is how we set up a Node.js development environment for a particular project:

  • Installing NPM and Node.js
    To get things started you need to install npm, and Node.js. You can easily do that by downloading them from here.

 

  • Create a project folder, and navigate to it using your favorite terminal. Then create a package.json file in that folder, a file that will save all the libraries you use. Using the terminal, run the command $ npm init . Write a few details about your project, and that’s it.

 

  • The web framework
    This should be project based, but we recommend Express.js as it the most mature and widely used framework for Node.js. From the Terminal, navigate to the folder where you will build your project, and run $ npm install express –save

 

  • The testing framework
    To deliver robust code, you need to write tests. Node.js has a lot of testing frameworks available, but Mocha.js is by far the most used and proven test framework out there. To install, run $ npm install –save-dev mocha .Here we also need to mention the assertion library that can make life even easier. We recommend Chai.js, as it exposes all interfaces of asset, should and expect. Just run $ npm install –save-dev chai  in your terminal to install.

 

  • The IDE
    We’ve chosen Visual Studio Code over the more widely preferred Sublime Text, as it has a native support for Node.js debugging. Also, it is pretty easy for newer, less experienced developers to adopt it. You can get this free piece of software from here.

 

  • The linter
    We decided to go with ESLint, as it is the most popular linter for Javascript and has a plugin for linting Node.js code. It is recommended that you install ESLint in the project folder that you work in, but you could also install it globally. To install it for your project, run $ npm install eslint –save-dev  in your terminal. To make it aware of Node.js and Mocha you need to install these Eslint plugins as well:
    – Eslint-node: $ npm install –save-dev eslint-plugin-node
    – Eslint-mocha: $ npm install –save-dev eslint-plugin-mocha

 

  • The coding standards:
    The Airbnb coding standard is one of the most popular out there, for JavaScript and Node.js. That would help you write clean code, that will be easier to understand later on, or by other people. In your terminal, run $ ./node_modules/.bin/eslint –init  and choose Airbnb as your coding standard. After you do that, you need to open the newly .eslintrc.json  file that was created, and add “mocha” and “node” in the file. Basically that file needs to look like this, if you saved the file using JSON format:
{
	"extends": "airbnb-base",
	"plugins": {
		"import",
		"mocha",
		"node"
	}
}
  • Install the ESLint plugin in VS Code
    Go into the VS Code app, and navigate to its Extensions page. Search after “eslint”, and install the first result from the list. Restart VS Code.

Now you should be all set up and start coding. Enjoy!

Privacy Preference Center