Both Node.js and the browser use JavaScript as their programming language, and both rely on a JavaScript engine to execute code. However, the environments they run in are vastly different.
Understanding what Node.js can and cannot do compared to the browser is crucial for avoiding confusing errors early on.
In a browser, JavaScript is deeply tied to the visual webpage. You have access to the document object, allowing you to manipulate HTML, add CSS classes, and listen to button clicks.
Node.js has NO DOM. Because Node.js runs on a backend server, there is no graphical user interface (GUI) or webpage to manipulate.
// ❌ Fails in Node.js
const header = document.getElementById("main-title");
header.style.color = "blue";
// ReferenceError: document is not defined
In the browser, the top-level scope is the window object. It contains built-in functions like alert(), prompt(), and standard Web APIs.
In Node.js, the top-level scope is called global. There is no window object in Node.
// ❌ Fails in Node.js
window.alert("Hello!"); // ReferenceError: window is not defined
// ✅ Works in Node.js
global.console.log("This works just like console.log()");
// Functions like setTimeout exist in both environments!
setTimeout(() => {
console.log("Time is up!");
}, 1000);
For security reasons, JavaScript running in a browser is strictly "sandboxed." It cannot freely read, write, or delete files on the user's computer. Imagine if visiting a website allowed JavaScript to secretly read your tax documents!
Node.js has full file system access.
Since Node.js runs on your server (or local machine), it acts like a normal application (like Python or C). It has a built-in fs (File System) module that gives you total control over the server's hard drive.
const fs = require('fs');
// ✅ This reads a text file from the server's hard drive!
fs.readFile('secrets.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
require() vs <script> TagsIn traditional browser JavaScript, multiple files are usually connected by loading them via HTML <script> tags.
Node.js uses a highly structured Module System (CommonJS natively, and ES Modules optionally). Every file in Node is treated as a separate, isolated module. You bring in code from other files using the require() function.
// Importing Node's built-in HTTP module to create a web server
const http = require('http');
// Importing a custom file you wrote
const myFunctions = require('./myFunctions.js');
| Feature | Browser | Node.js |
|---|---|---|
| Primary Purpose | Creating interactive UIs | Building backend servers / APIs |
| Global Object | window |
global |
| DOM Manipulation | Yes (document.getElementById()) |
No |
| File System Access | Strictly prohibited (sandboxed) | Full read/write access |
| Module System | ES Modules (import/export) |
CommonJS (require) & ES Modules |
| CORS Limitations | Yes | No (Node can fetch from anywhere) |
What is the name of the top-level object in Node.js (equivalent to the browser's window)?