Skip to main content

Command Palette

Search for a command to run...

Setting Up Your First Node.js Application Step-by-Step

Updated
3 min readView as Markdown

Let's do this in a quick, fun way with brevity.

Installing Node.js

For the sake of brevity, I am refraining from explaining or expanding upon inner workings of Node.js. If you want to learn more about it, please refer to my other article here. So with the basics of nodejs out of the way, let's install node.js on our device. I have tried to keep this article OS agnostic.

Here are the steps:

  1. Visit the nodejs website: https://nodejs.org. From there on, it's pretty straight forward. The directives are there with clarity, and you require only a bit of attention to install nodejs on your device.

  2. The button labelled with text `Get Node.js` or something very similar is what will lead you to the download route of the nodejs.

  3. On the download page, there are a bunch of options to choose from depending upon the system/OS of your device.

  4. The options present there are regarding the way. The "chocolatey" way or the "pre-built" way. Again, I'm not going to delve deeper into these methods themselves. Just follow the directives.

  5. If you chose the pre-built method, you would need to run the file that has just been installed.

Regardless of the way you took you should have nodejs installed on your device by now.

Checking Installation

This is a crucial step here. We would need to check whether the nodejs is installed securely or there i an error in need of a fix.

  1. Open the terminal.

  2. And then Enter the code below in your terminal:

node -v

The output must be a of this format: vxxxxxxx where x is the version number. Something like 24.6.2 or similar.

Node REPL

The node:repl module provides a built-in Read-Eval-Print Loop (REPL) implementation that functions as an interactive JavaScript shell within Node.js. Basically it allows us to write and execute Javascript code in the terminal, without the need to create a .js file.

Let's see how to use it for ourselves.

  1. Launching the terminal shell: Simply open the command console, or our reliable friend the terminal. Run the following command:

    node
    
  2. The prompt flag will now change to >. This means that any valid Javascript can be executed from the terminal itself.

  3. For example:

    >const message = "Hello Buddies"; console.log(message);
    Hello Buddies
    

The JS file

Creating a js file is eerily similar to creating a .txt file.

Create a new file and simply end it with '.js' extension. It's that simple.

For our sake, let's agree on naming the file hello.js.

const message = "Hello Buddies"; 
console.log(message);

Now to run it in a node environment, the following steps are to be followed:

  1. You can use command prompt or the terminal like we have done before.

  2. Navigate to the folder where your .js is located.

  3. Now in order to run the js code, node will take the helm. The command for executing the Javascript code is:

    node hello.js
    

The output inevitably prints Hello Buddies. This is how we execute a Javascript code using node command.

First Server

If you're aware, even a little bit, you would've caught the code for a node server on the nodejs.org.

This is the js (Javascript) code:

// server.mjs

Conclusion

This is where we'll part ways for now. Hope you learnt something. Node is a powerful tool and this is where your journey starts