Loading

Quipoin Menu

Learn • Practice • Grow

node-js / Your First Node.js Program
tutorial

Your First Node.js Program

You've installed Node.js – now it's time to write your first program! The tradition in programming is to start with "Hello, World!", and we'll do exactly that. But we'll also explore different ways to run Node.js code.

Three Ways to Run Node.js Code

  1. REPL (Read-Eval-Print Loop): Interactive mode where you type code and see results immediately.
  2. Running a JavaScript file: Write code in a file and execute it with `node`.
  3. Using a code editor: Write and run code from VS Code or other editors.

Think of REPL as a playground where you can test small pieces of code instantly. Think of file execution as writing a recipe that you can run anytime.

Method 1: Using Node.js REPL

Open your terminal and type:
node

You'll see a `>` prompt. Now you can type JavaScript directly:
> console.log('Hello, Node.js!');
Hello, Node.js!
undefined
> 2 + 3
5
> const name = 'Node';> console.log(`Welcome to ${name}!`); Welcome to Node!
> .exit <!-- To exit REPL -->

REPL is great for testing small code snippets, trying out JavaScript features, or debugging.

Method 2: Running a JavaScript File

Create a new file called `index.js` (or any name) and add this code:
<!-- Simple hello world -->
console.log('Hello, World!');

<!-- Working with variables -->
const name = 'Node.js';
const version = 'v18';
console.log(`Welcome to ${name} ${version}!`);

<!-- Simple calculation -->
const a = 10;
const b = 20;
console.log(`Sum of ${a} and ${b} is ${a + b}`);

<!-- Using built-in modules -->
const os = require('os');
console.log('Your OS:', os.platform());
console.log('CPU Cores:', os.cpus().length);

Now run it:
node index.js

You'll see output like:
Hello, World!
Welcome to Node.js v18!
Sum of 10 and 20 is 30
Your OS: win32 (or darwin/linux)
CPU Cores: 8

Method 3: Using VS Code

  1. Open VS Code.
  2. Create a new file (`Ctrl+N` or `Cmd+N`).
  3. Write your JavaScript code.
  4. Save the file (e.g., `app.js`).
  5. Open the integrated terminal (`Ctrl+`` or `Cmd+``).
  6. Run `node app.js`.

Understanding the Output

CodeWhat It Does
`console.log()`Prints to the terminal (like `print` in other languages).
`require('os')`Imports built-in modules (we'll learn more later).
Template literals `` `Hello ${name}` ``Modern way to embed variables in strings.

Creating a Simple HTTP Server

Let's create something more exciting – a web server! Create `server.js`:
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, World! This is my first Node.js server!');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

Run it:
node server.js

Now open your browser and go to `http://localhost:3000`. You'll see your message!

Two Minute Drill

  • Use REPL (`node` in terminal) for quick experiments.
  • Create `.js` files and run them with `node filename.js`.
  • `console.log()` is your best friend for debugging and output.
  • Use `require()` to include built-in modules like `os` and `http`.
  • Node.js can create web servers with just a few lines of code!

Need more clarification?

Drop us an email at career@quipoinfotech.com