Fullstack CourseLearn by building
Back to week 1

Topic

Node.js runtime, modules & npm

Definition

Node.js is a JavaScript runtime built on the V8 engine that executes JavaScript outside the browser and provides operating-system APIs for files, processes, networking, environment variables, and package loading.

In simpler words

Node runs JavaScript on the server. It gives you server-side APIs and a long-running process; npm installs packages and runs package scripts.

Nest and Express both run inside Node, so runtime, module, package, and environment behavior still matters after a framework is added.

After this you can

  • Distinguish Node, npm, Express, and Nest
  • Explain ESM imports versus CommonJS require
  • Read package.json scripts, dependencies, and devDependencies
  • Use process.env without committing secrets or assuming every value exists

Runtime, process, and server-side APIs

Definition

A Node process is an operating-system process running the V8 JavaScript engine plus Node APIs such as process, fs, path, streams, timers, and networking.

In simpler words

V8 executes the language; Node adds the server toolbox and keeps the process alive while work such as an HTTP listener remains open.

A browser supplies document, window, and DOM APIs. Node instead supplies process, Buffer, file-system APIs, sockets, and server-side streams.

process.argv contains command-line arguments, process.cwd() is the current working directory, and process.env contains string-valued environment variables. Validate required configuration at startup.

A Node API normally starts one long-lived process. Uncaught exceptions and unhandled promise rejections can terminate that process.

Validate configuration before listening

const port = Number(process.env.PORT ?? 3001);

if (!Number.isInteger(port) || port <= 0) {
  throw new Error('PORT must be a positive integer');
}

console.log({ cwd: process.cwd(), port });

Environment variables are strings or undefined. Convert and validate them once at application startup.

Modules, package.json, and npm

Definition

A JavaScript module exposes values through exports and consumes other modules through imports; package.json declares the package boundary, scripts, dependencies, and module conventions.

In simpler words

Split code into files with explicit imports. Let package.json describe how the app is installed, built, and started.

Modern TypeScript projects normally write ESM-style import/export and compile according to tsconfig.json. CommonJS uses require/module.exports.

dependencies are needed by the running application; devDependencies support development and build work. The lockfile records an exact resolved dependency graph.

Run repository scripts through the workspace package manager. In this monorepo, pnpm filters select an app or package without changing dependency boundaries.

Module and package script

// env.ts
export const port = Number(process.env.PORT ?? 3001);

// main.ts
import { port } from './env.js';

// package.json
{
  "scripts": {
    "build": "tsc -p tsconfig.json",
    "start": "node dist/main.js"
  }
}

TypeScript compiles source modules; Node executes the emitted JavaScript selected by the start script.

Keep in mind

  • Validate environment values at startup and keep secrets out of source control.
  • Use the repository package manager and scripts instead of ad hoc global installs.
  • When module loading fails, inspect package.json and tsconfig.json before changing import syntax randomly.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥ 80% (enforced by the API) to save progress.

Checking your session…

10 questions · concept 3 · syntax 2 · practical 3 · logic 2

Concept1. Which statement best defines Node.js runtime, modules, and npm?
Syntax2. Which code-level choice matches Node.js runtime, modules, and npm?
Practical3. A reviewer spots a bug related to Node.js runtime, modules, and npm. What is the right fix?
Logic4. Which reasoning correctly explains Node.js runtime, modules, and npm?
Concept5. Which boundary does correct use of Node.js runtime, modules, and npm preserve?
Practical6. What is the safest next step when applying Node.js runtime, modules, and npm?
Syntax7. Which implementation direction matches the rule for Node.js runtime, modules, and npm?
Logic8. Which consequence follows from applying Node.js runtime, modules, and npm correctly?
Concept9. Which claim about Node.js runtime, modules, and npm is true in this Nest + Postgres monorepo?
Practical10. Which team practice best demonstrates Node.js runtime, modules, and npm?