Skip to content
Node.js Basics — Complete Beginner's Guide to JavaScript on the Server

Node.js Basics — Complete Beginner's Guide to JavaScript on the Server

DodaTech Updated Jun 6, 2026 6 min read

Node.js is a JavaScript runtime built on Chrome’s V8 engine that lets you run JavaScript outside the browser — on servers, command-line tools, and even IoT devices — with access to the file system, network, and operating system.

What You’ll Learn

By the end of this tutorial, you’ll install Node.js, create and run scripts, use npm to manage dependencies, understand CommonJS and ES modules, and work with global objects like process and Buffer.

Why Node.js Basics Matters

Node.js powers the backend of applications like Netflix, PayPal, and LinkedIn. At DodaTech, Doda Browser uses Node.js for its sync server and bookmark API, while DodaZIP relies on Node.js for its cloud file conversion pipeline. Understanding Node.js opens doors to full-stack JavaScript development — one language from database to browser.

Node.js Learning Path

    flowchart LR
  A[Node.js Basics] --> B[Async JS]
  B --> C[Core Modules]
  C --> D[Express.js]
  D --> E[Database]
  E --> F[Advanced]
  A --> G{You Are Here}
  style G fill:#f90,color:#fff
  
Prerequisites: Solid JavaScript fundamentals (variables, functions, objects, arrays). Basic HTTP knowledge helps. No server experience needed.

How Node.js Works (The “Why” First)

Think of Node.js as JavaScript without the browser training wheels. In the browser, JavaScript can’t touch your files or run background processes (for security). Node.js removes those limits — your JavaScript can read files, create servers, talk to databases, and run operating system commands.

    flowchart TD
  A[JavaScript Code] --> B[V8 Engine]
  B --> C[Compiles to Machine Code]
  D[libuv] --> E[Event Loop]
  D --> F[File I/O / Network]
  A --> D
  

Node.js uses an event-driven, non-blocking I/O model. That means when you ask it to read a file, it doesn’t sit and wait — it starts reading and immediately moves on to the next task, coming back when the file is ready. This is why Node.js handles thousands of concurrent connections with ease.

Installation & Setup

# Download from nodejs.org (LTS version recommended)
node --version    # e.g., v22.0.0
npm --version     # e.g., 10.0.0

# Run JavaScript files
node app.js
node --watch app.js  # Auto-restart on changes (Node 18+)

REPL — Interactive Playground

$ node
> console.log("Hello Node!");
Hello Node!
> 2 + 2
4
> .exit

The REPL (Read-Eval-Print Loop) is like a sandbox — type JavaScript, see results immediately. Great for testing snippets.

Your First HTTP Server

// hello.js
const http = require("node:http");

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/html" });
  res.end("<h1>Hello Node.js!</h1>");
});

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

Line by line:

  1. require("node:http") — imports the built-in HTTP module
  2. http.createServer(...) — creates a server that handles requests
  3. The callback (req, res) => { } runs every time a request comes in
  4. res.writeHead(200, ...) — sets the HTTP status code and headers
  5. res.end(...) — sends the response and closes the connection
  6. server.listen(3000, ...) — starts the server on port 3000

Run node hello.js and open http://localhost:3000.

npm — Node Package Manager

npm init -y                # Create package.json
npm install express        # Install a package
npm install -D jest        # Dev dependency
npm uninstall express      # Remove
npm update                 # Update all packages
npm audit                  # Security audit
npm run dev                # Run a script from package.json

package.json Explained

{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node index.js",
    "dev": "node --watch index.js",
    "test": "jest"
  },
  "dependencies": {
    "express": "^4.18.0"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}

Module Systems

CommonJS (Default)

// math.js
function add(a, b) { return a + b; }
module.exports = { add };

// index.js
const math = require("./math");
console.log(math.add(2, 3));  // 5

ES Modules (Modern)

// package.json
{ "type": "module" }
// math.js
export function add(a, b) { return a + b; }

// index.js
import { add } from "./math.js";
console.log(add(2, 3));  // 5

ES Modules are the official JavaScript standard. CommonJS is Node.js’s original system. Use ES Modules for new projects.

Common Mistakes

1. Blocking the Event Loop

Don’t use fs.readFileSync in a server handler. Synchronous operations block ALL requests until they complete.

2. Forgetting Error Handling in Callbacks

fs.readFile("file.txt", (err, data) => {
  if (err) return console.error(err);  // Always check!
});

3. Hardcoding Configuration

Use environment variables with dotenv instead of hardcoding secrets.

4. Installing Everything as Regular Dependencies

Dev tools (testing, linting) belong in devDependencies, not dependencies.

5. Modifying node_modules

Changes are lost on npm install. Fork the package or submit a PR instead.

Practice Questions

1. What’s the difference between Node.js and browser JavaScript?

Node.js has no DOM, no window, but provides file system, network, and OS access. Both use V8 engine. Node uses CommonJS/ES modules, browsers use ES modules only.

2. What’s the event loop?

A mechanism that handles asynchronous callbacks. It lets Node.js perform non-blocking I/O — starting an operation, moving on, and returning when done.

3. What’s the difference between CommonJS and ES modules?

CommonJS uses require()/module.exports (synchronous, runtime). ES modules use import/export (asynchronous, static). ES modules require "type": "module" in package.json.

4. What does npm init -y do?

Creates a package.json file with default values — the starting point for any Node.js project.

5. Challenge: Create a CLI tool that reads a file and counts the number of words in it.

#!/usr/bin/env node
const fs = require("node:fs/promises");

async function wordCount(filePath) {
  try {
    const content = await fs.readFile(filePath, "utf8");
    const words = content.trim().split(/\s+/).length;
    console.log(`${filePath}: ${words} words`);
  } catch (err) {
    console.error("Error:", err.message);
    process.exit(1);
  }
}

const filePath = process.argv[2];
if (!filePath) {
  console.error("Usage: node wc.js <filename>");
  process.exit(1);
}
wordCount(filePath);

FAQ

What is npm?
npm is the default package manager for Node.js — installs, updates, and manages third-party packages and dependencies.
What is the REPL?
A Read-Eval-Print Loop — an interactive Node.js shell. Type node in the terminal to start it.
How do I auto-restart on file changes?
Use node --watch (Node 18+) or nodemon (third-party).
What’s the global object?
global — similar to window in the browser. __dirname, __filename, process, Buffer are global objects.

Try It Yourself

Create a CLI Calculator:

#!/usr/bin/env node
const [,, op, ...args] = process.argv;
if (!op || args.length < 2) {
  console.error("Usage: node calc.js <add|sub|mul|div> <num1> <num2>");
  process.exit(1);
}
const nums = args.map(Number);
if (nums.some(isNaN)) { console.error("Numbers only"); process.exit(1); }
let result;
switch (op) {
  case "add": result = nums.reduce((a, b) => a + b); break;
  case "sub": result = nums.reduce((a, b) => a - b); break;
  case "mul": result = nums.reduce((a, b) => a * b); break;
  case "div": result = nums.reduce((a, b) => a / b); break;
  default: console.error("Unknown operation"); process.exit(1);
}
console.log(`Result: ${result}`);

What’s Next

LessonDescription
https://tutorials.dodatech.com/backend/nodejs/nodejs-async/Callbacks, Promises, async/await
https://tutorials.dodatech.com/backend/nodejs/nodejs-core-modules/File system, HTTP, streams
https://tutorials.dodatech.com/backend/nodejs/express/Express.js web framework
JavaScriptCore JS language review
HTTPHTTP protocol fundamentals

What’s Next

Congratulations on completing this Nodejs Basics tutorial! Here’s where to go from here:

  • Practice daily — Consistency is more important than long study sessions
  • Build a project — Apply what you learned by building something real
  • Explore related topics — Check out other tutorials in the same category
  • Join the community — Discuss with other learners and share your progress

Remember: every expert was once a beginner. Keep coding!

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro