In this section, we will guide you through writing your first Node.js program. This will help you get familiar with the basic structure and syntax of Node.js applications.

Objectives

  • Understand the basic structure of a Node.js program.
  • Learn how to run a Node.js script.
  • Get introduced to the console.log function for output.

Steps to Create Your First Node.js Program

  1. Create a New Directory

First, create a new directory for your Node.js project. This will help you keep your files organized.

mkdir my-first-node-app
cd my-first-node-app

  1. Initialize a New Node.js Project

Initialize a new Node.js project using npm init. This will create a package.json file, which is essential for managing your project dependencies.

npm init -y

The -y flag automatically answers "yes" to all prompts, creating a package.json file with default settings.

  1. Create Your First JavaScript File

Create a new file named app.js in your project directory. This file will contain your first Node.js program.

touch app.js

  1. Write Your First Node.js Program

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

// app.js

// This is a simple Node.js program that prints "Hello, World!" to the console.
console.log("Hello, World!");

  1. Run Your Node.js Program

To run your Node.js program, use the node command followed by the name of your JavaScript file.

node app.js

You should see the following output in your terminal:

Hello, World!

Explanation of the Code

  • console.log("Hello, World!");: This line of code prints the string "Hello, World!" to the console. The console.log function is a built-in function in Node.js that outputs messages to the terminal.

Practical Exercise

Exercise 1: Modify the Program

Modify the app.js file to print your name instead of "Hello, World!".

Solution

// app.js

// This is a simple Node.js program that prints your name to the console.
console.log("Hello, [Your Name]!");

Replace [Your Name] with your actual name. Save the file and run it again using the node command.

Exercise 2: Add More Output

Add another console.log statement to print your favorite programming language.

Solution

// app.js

// This is a simple Node.js program that prints your name and favorite programming language to the console.
console.log("Hello, [Your Name]!");
console.log("My favorite programming language is JavaScript.");

Save the file and run it again using the node command.

Common Mistakes and Tips

  • Syntax Errors: Ensure that you have correctly typed the code. JavaScript is case-sensitive, so console.log must be in lowercase.
  • File Naming: Make sure your file is named app.js and is in the correct directory when you run the node command.
  • Node.js Installation: Ensure that Node.js is installed on your system. You can check this by running node -v in your terminal.

Summary

In this section, you learned how to:

  • Create a new Node.js project directory.
  • Initialize a Node.js project with npm init.
  • Write and run a simple Node.js program that prints output to the console.

You are now ready to move on to more complex Node.js programs and explore its powerful features. In the next section, we will dive into the Node.js REPL (Read-Eval-Print Loop) to interactively execute JavaScript code.

Node.js Course

Module 1: Introduction to Node.js

Module 2: Core Concepts

Module 3: File System and I/O

Module 4: HTTP and Web Servers

Module 5: NPM and Package Management

Module 6: Express.js Framework

Module 7: Databases and ORMs

Module 8: Authentication and Authorization

Module 9: Testing and Debugging

Module 10: Advanced Topics

Module 11: Deployment and DevOps

Module 12: Real-World Projects

© Copyright 2024. All rights reserved