bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Node.js/Node.js Tutorial
Node.js•Node.js Tutorial

Node.js vs Browser

Key Differences

Node.js and browsers both run JavaScript, but they have different environments and capabilities.

Node.js is designed for server-side development, while browsers are for client-side applications.

  • APIs: Node.js provides APIs for file system, networking, and OS, which browsers do not. Browsers provide DOM, fetch, and UI APIs not available in Node.js.
  • Global Objects: Node.js uses global ; browsers use window or self .
  • Modules: Node.js uses CommonJS ( require ) and ES modules ( import ); browsers use ES modules or plain <script> tags.
  • Security: Browsers run in a sandbox with limited access; Node.js has full access to the file system and network.
  • Event Loop: Both environments use an event loop, but Node.js has additional APIs for timers, process, etc.
  • Environment Variables: Node.js can access environment variables ( process.env ); browsers cannot.
  • Package Management: Node.js uses npm/yarn; browsers use CDNs or bundlers.

This page explains the key differences, with examples for each environment.

Global Objects

// Node.js
global.mylet = 42;
console.log(global.mylet); // 42
// Browser
window.mylet = 42;
console.log(window.mylet); // 42

File Access

// Node.js
const fs = require('fs');
fs.readFile('myfile.txt', 'utf8', (err, data) => {
 if (err) throw err;
 console.log(data);
});
// Browser
// Not allowed for security reasons

HTTP Requests

// Node.js
const https = require('https');
https.get('https://example.com', res => {
 let data = '';
 res.on('data', chunk => data += chunk);
 res.on('end', () => console.log(data));
});
// Browser
fetch('https://example.com')
.then(response => response.text())
.then(console.log);

Modules

// Node.js (CommonJS)
const fs = require('fs');
// Node.js & Browser (ES Modules)
// import fs from 'fs'; // Node.js only, not browser
// import _ from 'https://cdn.jsdelivr.net/npm/lodash-es/lodash.js'; // Browser

Comparison Table

FeatureNode.jsBrowser
File System AccessYesNo
Networking (TCP/UDP)YesNo
DOM AccessNoYes
Global Objectglobalwindow/self
ModulesCommonJS/ESMESM/Scripts
Environment VariablesYes ( process.env )No
SecurityFull OS accessSandboxed
Package Managementnpm/yarnCDN/Bundler

Previous

Node.js JavaScript Requirements

Next

Node.js Command Line Usage