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 package scripts through the workspace package manager. In a pnpm monorepo, 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 project’s 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…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. What is Node.js in a server project?
Concept
2. What does npm primarily manage?
Syntax
3. How should process.env values be treated at startup?
Practical
4. Why keep dependencies and devDependencies separate?
Logic
5. Which claim about package.json scripts is true?
Concept
6. What does require/import resolve in a Node app?
Practical
7. Which startup practice is safest for config?
Syntax
8. What is node_modules?
Logic
9. Which statement about CommonJS vs ESM is accurate for Node courses?
Concept
10. Why pin versions with a lockfile?
Practical
11. Given this package.json, which command runs the compiled production server?
Conceptintermediate
{
  "scripts": {
    "start": "node dist/main.js",
    "start:dev": "nest start --watch",
    "build": "nest build"
  }
}
12. This startup config reads a port from the environment. What is the bug?
Syntaxadvanced
const port = process.env.PORT;
app.listen(port + 1);
13. Where does Node resolve each require?
Practicalintermediate
const util = require('./util');
const lodash = require('lodash');
14. Why prefer npm ci over npm install in CI?
Logicadvanced
# CI pipeline step
npm install
15. Why might this line fail at runtime in a plain CommonJS Node project?
Conceptintermediate
// package.json has no "type": "module"
import express from 'express';

Checking your session…