Node.js vs the browser
1. Where the code runs
| Environment | Where it runs | Main purpose |
|---|---|---|
| Node.js | On a server or locally on a machine | Server logic, APIs, automation, utilities |
| Browser | On the client side (in the user's browser) | UI work, DOM, user interaction |
2. Available APIs and environment
| Capability | Node.js | Browser |
|---|---|---|
| Filesystem access | Yes, fs | None |
| Network access (TCP, HTTP server) | Yes, http, net | Partial: only HTTP requests via fetch |
| DOM access (HTML, page elements) | None | Yes |
Web APIs (fetch, localStorage, WebSocket) | Partial (Node 18+ added some) | Full access |
| Global objects | global, process, __dirname | window, document, navigator |
3. Modules and dependencies
| Feature | Node.js | Browser |
|---|---|---|
| Module system | CommonJS (require) and ES Modules (import) | ES Modules only |
| Package manager | npm, pnpm, yarn | None (needs a bundler - Webpack, Vite, etc.) |
| Importing packages | Directly from node_modules | Via bundling into a single file |
4. Architecture and asynchrony
- Both environments run the V8 engine, but Node.js wraps it with its own system APIs.
- Node.js uses the event loop for asynchronous I/O: reading files, requests, processes.
- In the browser, the event loop handles UI events: clicks, timers, requests to the server.
5. Security and access
| Aspect | Node.js | Browser |
|---|---|---|
| System access | Yes | None |
| Code isolation | None (code can access the disk and network) | Yes (sandbox) |
| Trust level | Server-side code | Untrusted environment (the user's browser) |
6. Examples of code differences
Node.js:
javascript
// Working with files
const fs = require('fs');
fs.writeFileSync('text.txt', 'Hello from Node.js!');Browser:
javascript
// Working with the DOM
document.body.innerHTML = '<h1>Hello from Browser!</h1>';Quick summary
| Criterion | Node.js | Browser |
|---|---|---|
| Runtime environment | Server | Client |
| DOM / UI | None | Yes |
| Filesystem access | Yes | None |
| Security | Less restricted | Sandboxed |
| Main job | Server logic, APIs | Rendering the UI, UX |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.