DEV Community

Cover image for Getting Started with Node.js: Setting Up and Running Your First Application
Shefali
Shefali

Posted on • Updated on • Originally published at shefali.dev

Getting Started with Node.js: Setting Up and Running Your First Application

Introduction

Node.js has rapidly become one of the most popular platforms for building server-side applications using JavaScript. It stands as an excellent option for individuals seeking to explore the realm of server-side programming. In this blog post, I will walk you through the process of setting up Node.js and getting started with your first application.

But What is Node.js?

Okay, Let me tell you.

What is Node.js?

Node.js is an open-source, cross-platform runtime environment that allows you to execute JavaScript code on the server side.

To put it simply, it allows developers to use JavaScript not just within web browsers but also on the server side and build and run applications using the same programming language, JavaScript, for both frontend (client side) and backend (server side).

So by learning one programming language, JavaScript, you can build dynamic and interactive web applications.

Okay, now I understand what is node.js, but how do I use it?

So let's continue with installing node.js and npm.

Hey, wait, what is npm?

Before installing node.js let's see what is npm.

What is npm?

npm(Node Package Manager), is a command-line tool and a central repository for JavaScript packages and libraries.

By using npm, you can easily install and manage modules.

Now what are modules?

Modules are like code libraries, written by other developers. You can use these modules to simplify development. This approach helps in organizing your codebase, making it more maintainable, and reliable.

At this instant, let's get back to npm.

With npm, you can quickly integrate third-party functionalities into your projects. Whether you need to add advanced features to your applications or simplify your development workflow, npm is like a superstore filled with free tools for your Node.js apps. It gives you easy access to a huge bunch of free stuff made by other developers, without writing all the code from scratch.

Now let's install node.js.

Installing Node.js

Before you can start using Node.js, you need to install it on your machine.

Following are the steps to install Node.js and npm:

1: Download Node.js

To download Node.js, visit the official Node.js website (https://nodejs.org) and download the appropriate installer for your operating system (Windows, macOS, or Linux).

I'm downloading this for Windows.

Getting Started with Node.js

2: Install Node.js

Double-click on the downloaded .msi file and give access to run the application.

Then click the “Next” button and the installation process will start.

Getting Started with Node.js

Accept the license agreement and click on the "Next" button.

Getting Started with Node.js

Choose the destination path and click on the "Next" button.

Getting Started with Node.js

Now it will install some features automatically, click on the "Next" button.

Getting Started with Node.js

Click on "Next" and the setup is ready to install, click on the "Install" button.

Getting Started with Node.js

Click on the "Finish" button.

3: Verify Installation

Now to verify that Node.js and npm were installed successfully open your terminal (command prompt on Windows) and enter the following commands:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

The commands will return the version number of the node and npm.

Getting Started with Node.js

Hooray! You've successfully installed the node and npm on your machine.

Now, let's write your first node.js program.

First Node.js Program

In Node.js, the applications can be written using the following two methods:

  • console-based method
  • web-based method

In this guide, we will use the console-based method.

1: Create a New Directory

Open your terminal and navigate to the directory where you want to create your Node.js project. Create a new directory for your project using the following commands:

mkdir first-nodejs-project
cd first-nodejs-project
Enter fullscreen mode Exit fullscreen mode

Getting Started with Node.js

2: Initialize your Project

Run the following command to initialize your project. This will create a package.json file in your directory.

npm init
Enter fullscreen mode Exit fullscreen mode

You can choose the default options or edit them. All the project information will be in your package.json file.

3: Write Your First Program

Create a new file named index.js in your root directory. Open the file in a code editor and add the following code:

console.log("Hello, Node.js!");
Enter fullscreen mode Exit fullscreen mode

4: Run Your Program

In your terminal, run the following command.

node index.js
Enter fullscreen mode Exit fullscreen mode

You should see the output Hello, Node.js! in your terminal.

Getting Started with Node.js

Congratulations! You've just run your first Node.js program!

Conclusion

In this post, you learned what are Node.js and npm, how to install them as well as how to create and run a simple Node.js program.

This is just the beginning of your Node.js journey. As you delve deeper into Node.js, you'll explore its powerful features, build web servers, interact with databases, and create APIs.

You can check out https://nodejs.dev/en/learn/ to learn more about Node.js and to continue your learning.

Thanks for reading!

Keep Coding!

For more content like this click here!
Buy Me A Coffee

Top comments (0)