Tsallaka zuwa abun ciki
Justice Tech School

Modules

Split code into files. CommonJS uses require and module.exports. Modern code uses import and export in .mjs files or with "type": "module" in package.json. Built in modules such as path, os and fs need no install.

Example
const path = require("path");
const os = require("os");
console.log(path.join("a", "b", "c.txt"));
console.log(path.basename("/x/y/file.js"), path.extname("file.js"));
console.log(typeof os.cpus().length);
const { add } = { add: (a, b) => a + b };
console.log(add(2, 3));

Result

a/b/c.txt
file.js .js
number
5

Exercise

Load a module with CommonJS.

const fs = ("fs");

Quiz Test

Which keyword imports in ES modules?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.