As a project grows, keeping all your code in one file makes it hard to find things, hard to edit, and hard to track down errors. In Node.js, you can split files into modules, export with module.exports, and pull them back in with require().
// File: mathTools.js
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
module.exports = { add, multiply };
// File: app.js
const mathTools = require('./mathTools');
console.log(mathTools.add(5, 3));
console.log(mathTools.multiply(5, 3));mathTools.js defines two functions and exports them as an object. In app.js, you pull it in with require('./mathTools') and call mathTools.add() and mathTools.multiply().
8 15Info
When importing a local file, if you leave out ./, Node.js might think it's a package instead. So for files inside your own project, use the ./filename format.