NODEJS Contents

Node.js vs Browser JavaScript

Explore the key differences between Node.js and browser JavaScript, including global objects, APIs, security boundaries, and runtime capabilities.

On this page

Same Language, Different Environment

Node.js and browser JavaScript share the same language syntax, but they run in completely different environments. The differences are not about JavaScript itself, but about the APIs and runtime capabilities available.

No DOM in Node

In the browser, you have access to the DOM (document, window, localStorage). In Node.js, these objects do not exist because there is no browser environment.

Example: Browser-Only Code

console.log(document.title);

This will throw an error in Node because document is undefined.

Node Global Objects

Node provides its own global objects such as:

  • process
  • Buffer
  • global
  • __dirname

File System Access

Browsers cannot read your local filesystem directly for security reasons. Node can, because it runs outside the browser sandbox.

Example: Reading a File in Node

import fs from 'fs';

const data = fs.readFileSync('./file.txt', 'utf-8');
console.log(data);

Networking Differences

Browsers use fetch or XMLHttpRequest and are restricted by CORS and sandboxing. Node can open raw TCP connections, act as a server, or perform backend-to-backend communication without browser restrictions.

Security Boundaries

Browser JavaScript runs in a sandbox for user protection. Node runs with system-level permissions. That means bugs in Node applications can have more serious consequences if not handled properly.

Module Systems

Modern browsers support ES modules. Node also supports ES modules, but historically used CommonJS. Backend projects often need to understand both systems.

Environment Variables

In Node, configuration typically comes from environment variables via process.env. In browsers, environment variables are usually injected during build time.

Performance Perspective

Browser performance concerns focus on rendering and user interaction. Node performance concerns focus on throughput, latency, and memory usage under load.

JS Note

The core JavaScript engine (V8 in Chrome and Node) is the same. The difference lies in the runtime environment and available APIs.

Production Insight

When moving code from frontend to backend, do not assume APIs are interchangeable. Always check whether an API is browser-specific or runtime-specific.

Next Step

Next, we will dive deeper into the V8 engine and understand how Node executes JavaScript internally.