Node.js has become a go-to technology for developers looking to build scalable, high-performance applications. We're big fans of Node.js around here and we use it on most of our projects, but getting started with it can feel overwhelming. This is the first article in a series dedicated to helping beginners learn all about it, based on internal tutorials created by some of our most senior developers, and the idea behind it is to cover the basics of setting up Node.js, understanding its key features, and getting familiar with foundational concepts required to start developing with it.

First things first: What is Node.js?

Node.js is a runtime environment that allows developers to execute JavaScript code outside a web browser. While JavaScript is traditionally used for front-end development, Node.js enables the use of JavaScript for back-end development, making it possible to build server-side applications, APIs, and more. Think of it as a way to run JavaScript code directly on your computer or server, without needing a browser.

Setting Up Node.js in the right way.

When setting up Node.js, you can download it directly from the official Node.js website. However, a recommended approach is to use Node Version Manager, or nvm. This tool allows you to manage multiple Node.js versions on your machine, making it easy to switch between versions for different projects. 

1. Installing nvm:

   Install nvm using the following command in your terminal:

 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

   

2. Installing Node.js:

   Once nvm is installed, you can install Node.js using:

nvm install node


 
 |You can specify a particular version if needed:

 nvm install 14.17.0

   

3. Switching Between Versions: 

   To switch between versions, use:

   nvm install 14.17.0

Using nvm allows you to maintain different Node versions for different projects, making your workflow more efficient.

Creating Your First Node.js Script

After setting up Node.js, write a basic script to get started. Open your preferred code editor (e.g., Visual Studio Code) and create a new file named index.js. Write the following code:

console.log("Hello, Node.js!");

To run the script, open your terminal, navigate to the folder containing index.js, and execute:

node index.js

You should see Hello, Node.js! printed in the terminal. This confirms that your Node.js setup is working correctly.

Understanding Asynchronous Programming

Node.js is known for its non-blocking, asynchronous programming model, meaning operations like reading files or making HTTP requests don’t block the execution of other code. This concept might be tricky at first, but it's essential for building efficient Node.js applications.

Consider the following example of reading a file using the fs module:

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {

    if (err) {

        console.error(err);

        return;

    }

    console.log(data);

});

This code reads the content of example.txt and prints it to the console. The readFile function takes a callback, which is executed once the file has been read. This pattern is known as a callback.

Using nvm and npm Together

During the setup, the speaker emphasized using additional tools like nvm for managing Node.js versions and npm for handling packages. For example, using nvm to install a specific version of Node.js can simplify dependencies and versioning across projects:


nvm install 12.18.3

nvm use 12.18.3

Moreover, npm (Node Package Manager) is automatically installed with Node.js and is used to manage and install external packages and libraries. This helps add functionality to your projects without reinventing the wheel.

Improving Asynchronous Programming with Async/Await

Instead of using callbacks, you can use Promises and the async/await syntax to make your code more readable and avoid "callback hell". Here’s how to read a file using async/await:

const fs = require('fs').promises;

async function readFileAsync() {

    try {

        const data = await fs.readFile('example.txt', 'utf8');

        console.log(data);

    } catch (err) {

        console.error(err);

    }

}

readFileAsync();

Using async/await makes your code look more sequential and easier to understand, especially when dealing with multiple asynchronous operations.

Node.js is a powerful tool that extends the use of JavaScript to the server side. By mastering Node’s core concepts such as asynchronous programming, using tools like nvm and npm, and understanding Promises and async/await, you’ll be on your way to building robust, scalable applications.

Stay tuned for part 2 of this series coming soon!