Node.js comes with several built-in modules like fs
, http
, path
, and os
.
Example using the fs
module to read a file:
const fs = require("fs"); fs.readFile("example.txt", "utf8", (err, data) => { if (err) { console.error(err); return; } console.log(data); });
Create math.js
:
exports.add = (a, b) => a + b; exports.subtract = (a, b) => a - b;
Use it in app.js
:
const math = require("./math"); console.log(math.add(5, 3));