NodeJS basics 101

A runtime environment, not a language!

Dhanraj Acharya
wineofbits
4 min readFeb 25, 2019

--

NodeJS Logo

NodeJS ecosystem is very big and it is still growing day by day. It is an asynchronous event driven environment that uses google’s v8 engine to run javascript. V8 is the same engine used in google chrome to run JavaScript.

NodeJS can also use chakra(developed by microsoft) or spidermonkey(by Mozilla). But Node uses V8(by google) as of now. Due to this competition, we get better engines each year so yay!

As it is asynchronous in nature, you should be able to leverage or handle it efficiently. So I highly recommend that once you finish this guide, you read about callback hell and promises.

This article will focus on covering the tools that will help you as a beginner to better understand the environment. Hovever, if you’re already a NodeJS developer then you might already know these.

The official docs explains NodeJS very efficiently in a concise manner. If you’re interested then give it 5 minutes of your time.

It should be clear now that NodeJS is a runtime environment, not a language. Javascript is the language which is used to program in NodeJS.

Javascript also has many varients but that is another topic.

NVM: Installing and managing node versions effortlessly

To use NodeJS, one has to actually install it and many times in a particular project, you have to use specific version of NodeJS. So first thing to do is install NVM.

Don’t be afraid to read the instructions from plain markdown text.

NVM: Installation

Use nodemon while developing nodeJS apps

When you’re developing the application then you have to restart the script each time if you use node app.js This is very cumbersome for debugging and developing. So use Nodemon, it restarts the server automatically whenever you make changes to your files.

To install it globally and start the app, use below commands,

Know your logs better!

One of the most important tool for a developer are logs. Even in production, we need logs to monitor and log important information. Checkout the guide given by risingstack.

Difference between module.exports and exports?

It is very likely that you will create your project in a modular fashion. So you will be creating multiple files and exporting different functions. To export it, you will use exports object.

module is a plain JavaScript object with an exports property. exports is a plain JavaScript variable that happens to be set to module.exports. At the end of your file, node.js will basically 'return' module.exports to the require function. A simplified way to view a JS file in Node could be this:

If you set a property on exports, like exports.a = 9;, that will set module.exports.a as well because objects are passed around as references in JavaScript, which means that if you set multiple variables to the same object, they are all the same object; so then exports and module.exports are the same object.
But if you set exports to something new, it will no longer be set to module.exports, so exports and module.exports are no longer the same object.

This great explanation was provided by an amazing developer goto-bus-stop.

So if you do something like exports={getUserById:getUserById}. It won’t work and you won’t get anything when you try to import it. while exports.getUserById = getUserById will work.

What is Event Loop?

I urge you to go through this video and know what exactly is an Event Loop. Please go through this. It is very important that I am saying this again and again. It will clear many things and give you Aha moments!

NodeJS ECMAScript features

This might not be useful to you immediately but if you know then it will come in handy in future.

You may want to use particular feature of ECMAScript and different versions of NodeJS ships different features. You can check the API docs for information on which features are supported by each version. But if you don’t want to go through the docs just to get the YES/NO answer like me, then Check this website to quickly track the progress of each feature.

https://node.green

As of this writing the current LTS version is 10.15.1, Latest stable version is 11.10 and Nightly build version is 12.

Thank you for reading.

Please leave comments if you have any suggestion/s or would like to add a point/s or if you noticed any mistake/typos!

P.S. If you found this article helpful, clap! 👏👏👏 [feels rewarding and gives the motivation to continue my writing].

Bonus

Javascript ES6 core features

Guide to Promises

--

--

Dhanraj Acharya
wineofbits

Full Stack Developer. I love experimenting with new tools and tech.