The Node Runtime

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript code outside the browser, enabling server-side development and scripting.

Illustration of The Node Runtime
Illustration of The Node Runtime

💡 What is a Runtime?

A runtime provides an environment where your code executes. Node.js includes the V8 engine plus additional APIs for system access like file I/O, networking, and more.

Key Components of Node Runtime

  • V8 Engine: Compiles and runs JavaScript code quickly.
  • Libuv: Handles asynchronous I/O operations like file system, network, and timers.
  • Node API: Provides APIs for file system, networking, process management, etc.
  • Event Loop: Manages asynchronous callbacks and non-blocking I/O.
Node Runtime vs Browser JavaScript
FeatureNode.js Runtime
Global Objectglobal
APIs AvailableFile system, Network, Child processes
Use CaseServer-side scripting, tooling
Module SystemCommonJS (require/export) & ES Modules
Browser APIsNot available (e.g., no DOM)

How Node Executes JavaScript

When you run node yourfile.js, Node:

  • Reads your JavaScript file.
  • Passes it to the V8 engine to compile and run.
  • Uses the event loop to handle asynchronous operations.

💡 Event Loop Simplified

Node's event loop lets your program handle many tasks concurrently without blocking the main thread.

Common Node Runtime Globals

  • global — The global object (like window in browsers).
  • process — Provides info about and control over the current Node process.
  • __dirname & __filename — Paths to the current directory and file.
  • require — Function to import modules.

⚠️ No Browser APIs

Node.js does not include browser-specific APIs like document, window, or localStorage. For these, use browser environments or tools like jsdom.

📌 Deep Dive: Basic Node Script

JAVASCRIPT
console.log('Running in Node.js!');
console.log('Current directory:', __dirname);
console.log('Node version:', process.version);
Output
Running in Node.js!
Current directory: /your/current/path
Node version: v18.0.0 (example)

💡 Summary

The Node runtime is a powerful environment that enables JavaScript to run outside browsers, providing access to system resources and asynchronous capabilities through its event-driven architecture.