@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.
@types/node, ESM vs CJS in Node is a free TypeScript Academy lesson on CoddyKit — 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, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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/nodeNode 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.
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, and the TypeScript Academy course includes 2 lessons in total. 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.
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; this is — lesson 1 of 2, so you can start here or from the beginning and move at your own pace.
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
- @types/node, ESM vs CJS in Node
- Runtime options (ts-node/tsx) & sourcemaps