Support Online
Skip to main content

Write and Run Your First Program with Node.js

In this guide, you'll learn how to write your first JavaScript application using Node.js.
We will walk through outputting output to the console, receiving command line input from the user, and reading system environment variables.
We will also create a mini command line tool (CLI) with error handling and dynamic argument handling.

What is Node.js and Why is it used?

Node.js is an open source JavaScript runtime that uses Google Chrome's V8 engine.
It allows us to run JavaScript codes outside the browser and is very efficient in I/O operations thanks to its asynchronous architecture.
For example; It is often preferred in real-time applications such as chat, video streaming or API servers.

Prerequisites

  • A system with Node.js installed (v10+ recommended)
  • Basic JavaScript knowledge

🧩 Developing a Node.js Program in 7 Steps

1️⃣ Output to Console

To start, let's write the classic "Hello World" application.

nano merhaba.js
console.log("Merhaba GenixNode!");
// Bu kod, terminale “Merhaba GenixNode!” yazar.
Çıktı:
Merhaba GenixNode!

2️⃣ Viewing Command Line Arguments

We use the process.argv object so that the program can receive input from the user.

console.log(process.argv);
node argumanlar.js bir iki üç

Output:

[ '/usr/bin/node', '/home/gelistirici/argumanlar.js', 'bir', 'iki', 'üç' ]

3️⃣ Extracting User Arguments

Just implement slice(2) method to get user inputs.

console.log(process.argv.slice(2));

Output:

[ 'bir', 'iki', 'üç' ]

4️⃣ Accessing Environment Variables

process.env is used to see the environment variables defined in the system.

console.log(process.env);
// Bu, sistemdeki tüm ortam değişkenlerini listeler.

To get only a specific variable:

console.log(process.env["HOME"]);

Output:

/home/gelistirici

5️⃣ Using User Argument as Environment Variable

Let the user take whatever variable he wants to see as an argument.

const args = process.argv.slice(2);
console.log(process.env[args[0]]);
// Kullanıcının girdiği değişkenin değerini getirir.

Run:

node goster.js HOME

Output:

/home/gelistirici

6️⃣ Displaying Multiple Variables

Use the forEach() loop to get multiple environment variables.

const args = process.argv.slice(2);
args.forEach(arg => console.log(process.env[arg]));

Run:

node goster.js HOME PATH USER

7️⃣ Managing Undefined Values

Let's show an error message to the user when an invalid variable is entered.

const args = process.argv.slice(2);

args.forEach(arg => {
const envVar = process.env[arg];
if (envVar === undefined) {
console.error(`Hata: "${arg}" ortam değişkeni bulunamadı.`);
} else {
console.log(envVar);
}
});

Output:

/home/gelistirici

Hata: "YANLIŞ" ortam değişkeni bulunamadı.

💡 Frequently Asked Questions (FAQ)

  1. What is the difference between console.log and console.error?

console.log writes normal output (stdout), console.error writes error messages (stderr).

  1. What does process.argv do?

It keeps the arguments entered by the user from the terminal as an array.

  1. What is process.env?

Provides access to operating system environment variables.

  1. Is there a more advanced way to catch errors?

Yes, you can manage errors more systematically with libraries such as try/catch or yargs, commander.


🏁 Conclusion

Now, in the Node.js environment, you have learned: ✅ Giving output to the console, ✅ Receiving data from the command line, ✅ Reading environment variables, ✅ Managing errors.

These skills form the basis for CLI tools and microservice applications. You can take your Node.js experience to a professional level by hosting your projects on the GenixNode Platform.