Skip to content

Pratama Digital

Your Digital Partner

Menu
  • Home
  • Download Area
  • Pratama Digital Net
  • Internet Speed Test
  • About Us
Menu

Getting Started with Node.js: A Basic Tutorial

Posted on September 16, 2023

node.js is a runtime environment that allows you to run JavaScript on the server-side. It’s designed to be lightweight and efficient, making it an excellent choice for building scalable and high-performance web applications. In this tutorial, we’ll cover the basics of Node.js, including installation, creating a simple application, and working with modules.

Prerequisites

Before we begin, make sure you have the following prerequisites installed on your computer:

  1. Node.js: Download and install Node.js from the official website (https://nodejs.org/). Node.js includes npm, the Node Package Manager, which we’ll use to manage dependencies.
  2. A Text Editor: You’ll need a code editor. Popular choices include Visual Studio Code, Sublime Text, or Atom.

Checking Node.js Installation

Open your terminal or command prompt and run the following command to verify that Node.js and npm are correctly installed:

node -v
npm -v

You should see the versions of Node.js and npm displayed in the terminal.

Creating Your First Node.js Application

Let’s create a simple Node.js application that prints “Hello, Node.js!” to the console.

Step 1: Create a Project Folder

Start by creating a folder for your project. Open your terminal, navigate to the desired location, and run:

mkdir nodejs-tutorial
cd nodejs-tutorial

Step 2: Initialize a Node.js Project

Inside your project folder, run the following command to create a package.json file, which will store your project’s metadata and dependencies:

npm init -y

The -y flag is used to accept all default settings. You can modify these settings later in the package.json file.

Step 3: Create a JavaScript File

Next, create a JavaScript file named app.js within your project folder:

touch app.js

Step 4: Write Your First Node.js Code

Open app.js in your code editor and add the following code:

// app.js
console.log(“Hello, Node.js!”);

 

This simple script logs the message “Hello, Node.js!” to the console.

Step 5: Run Your Node.js Application

In your terminal, navigate to the project folder and run your Node.js application:

node app.js

 

You should see the message “Hello, Node.js!” printed to the console.

Working with Modules

One of the strengths of Node.js is its module system, which allows you to organize your code into reusable modules. Let’s create a basic example of how to use Node.js modules.

Step 1: Create a New JavaScript File

Inside your project folder, create a new JavaScript file named greetings.js:

touch greetings.js

Step 2: Create a Module

Open greetings.js in your code editor and define a simple function that returns a greeting message:

// greetings.js
function sayHello(name) {
return `Hello, ${name}!`;
}

module.exports = sayHello;


In this code, we’ve created a function sayHello and exported it using module.exports.

Step 3: Use the Module in Your Application

Now, let’s modify app.js to use the greetings module:

// app.js
const sayHello = require(“./greetings”);

const message = sayHello(“Node.js”);
console.log(message);

In app.js, we import the sayHello function from the greetings module and use it to greet “Node.js.”

Step 4: Run Your Updated Application

Run your updated Node.js application:

node app.js

You should see the message “Hello, Node.js!” printed to the console, indicating that the module import was successful.

Conclusion

Congratulations! You’ve successfully created a basic Node.js application and learned how to work with modules. Node.js offers a wide range of modules and libraries that can help you build powerful server-side applications. As you continue to explore Node.js, you’ll discover its versatility and ability to handle various tasks, from web development to building APIs and even creating real-time applications.

Share this:

  • Facebook
  • X

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages

Tags

24h clock adobe adobe cc adobe cs master almalinux arduino autodesk basic ip address cad software calculator php connect java dns server download photoshop ecmp formula ipv4 innodb install iptables ipsec iptables java mysql linux firewall linux mint linux security load balance microsoft office myisam mysql mysql engine node.js nodejs nth office alternative openvpn pcc php port game online port mikrotik postgresql postgresql to mysql public dns rocky linux running text script php ubuntu LTS wireguard

Categories

  • Advertising
  • CAD software
  • Computer Component
  • Control Panel
  • Database
  • DNS
  • Download
  • File Server
  • Firewall
  • Graphic Design
  • Internet
  • IP Address
  • Linux OS
  • Load Balance
  • Mikrotik
  • Movie
  • Novel/Book
  • Office
  • OpenWRT
  • Processor & GPU
  • Programming
  • Routing
  • Security
  • Server
  • Tutorial
  • Video Editing
  • VPN
  • Web Programming
  • Windows OS
  • Wordpress

About Us

We are a company specializing in network configuration, MikroTik and computer installation services, as well as wifi setup. With experience dating back to 2017, we have gained the trust of numerous clients from various regions who rely on our services. Our expertise extends to catering to personal, office, institutional, and industrial needs.

Archives

  • December 2024
  • September 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages
©2025 Pratama Digital | Design: Newspaperly WordPress Theme