TypeScript Academy · Lesson

@types/node, ESM vs CJS in Node

@types/node provides typings for Node APIs, and tsconfig module options let you choose between CommonJS and ESM.

Lesson 1 of 28 steps

@types/node, ESM vs CJS in Node is a free TypeScript Academy lesson on CoddyKit. This is lesson 1 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The TypeScript Academy course includes 2 lessons in total.

Intro

Goal: Use @types/node for type safety and configure TS for CommonJS vs ES Modules.

Install typings

Install @types/node to get type definitions for built-in Node APIs like fs and path.

npm install --save-dev @types/node

Node globals

With @types/node, globals like process, __dirname, and __filename are recognized with types.

console.log(process.version);
console.log(__dirname);
console.log(__filename);

CJS example

CommonJS: Default in Node. Use require and module.exports.

// CommonJS style
const fs = require("fs");
const data = fs.readFileSync("file.txt", "utf8");
console.log(data);

ESM example

ESM: Modern syntax with import/export. Requires tsconfig "module": "ESNext" and "type": "module" in package.json.

// ES Module style
import fs from "fs";
const data = fs.readFileSync("file.txt", "utf8");
console.log(data);

tsconfig modules

Control module system in tsconfig.json. Switch module between CommonJS and ESNext.

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "CommonJS",
    "moduleResolution": "node"
  }
}

Typings check

Quick check: Why install @types/node?

Recap

Recap: @types/node adds type definitions. CommonJS uses require, ESM uses import. tsconfig decides the module system.

Free to start

Learn TypeScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
101
Lessons
352

Frequently Asked Questions

Is the “@types/node, ESM vs CJS in Node” lesson free?

Yes — the full text of “@types/node, ESM vs CJS in Node” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO. The TypeScript Academy course includes 2 lessons in total.

What will I learn in “@types/node, ESM vs CJS in Node”?

@types/node provides typings for Node APIs, and tsconfig module options let you choose between CommonJS and ESM. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 2.

How long does the “@types/node, ESM vs CJS in Node” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. @types/node, ESM vs CJS in Node
  2. Runtime options (ts-node/tsx) & sourcemaps
← Back to TypeScript Academy