Node.js5 min read

Your First Node.js Program

Write and run your first Node.js program. Learn the basics of creating and executing JavaScript files.

Sarah Chen
December 19, 2025
0.0k0

Your First Node.js Program

Let's write real Node.js code. No theory, just practice.

Hello World

Create hello.js:

console.log('Hello, Node.js!');

Run it:

node hello.js
# Output: Hello, Node.js!

That's it. You've written Node.js!

Working with Variables

// app.js
const name = 'Alice';
const age = 25;

console.log(`Name: ${name}, Age: ${age}`);

// Arrays and objects work the same
const skills = ['JavaScript', 'Node.js', 'Express'];
const user = { name: 'Bob', role: 'developer' };

console.log(skills);
console.log(user);

Getting User Input

// input.js
const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What is your name? ', (answer) => {
  console.log(`Hello, ${answer}!`);
  rl.close();
});

Command Line Arguments

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

console.log('Arguments:', args);
console.log('First arg:', args[0]);

Run: node args.js hello world

Output:

Arguments: [ 'hello', 'world' ]
First arg: hello

Simple Calculator

// calc.js
const args = process.argv.slice(2);
const num1 = parseFloat(args[0]);
const operator = args[1];
const num2 = parseFloat(args[2]);

let result;
switch (operator) {
  case '+': result = num1 + num2; break;
  case '-': result = num1 - num2; break;
  case '*': result = num1 * num2; break;
  case '/': result = num1 / num2; break;
  default: console.log('Unknown operator'); process.exit(1);
}

console.log(`${num1} ${operator} ${num2} = ${result}`);

Run: node calc.js 10 + 5

Reading a File

// readfile.js
const fs = require('fs');

const content = fs.readFileSync('hello.js', 'utf8');
console.log('File contents:');
console.log(content);

Key Takeaway

Node.js runs JavaScript files with node filename.js. You have access to process.argv for arguments, built-in modules like fs and readline, and all JavaScript features. Start simple, then build up!

#Node.js#Getting Started#Beginner