In Node.js, you can include functions or any other exports from other files using the require or import statements, depending on whether you are using CommonJS or ES modules. Here’s a detailed guide:
1. Using CommonJS (Default in Node.js)
To include functions from another file:
Step 1: Export the Function
In the file you want to export from (e.g., functions.js):
// functions.js function greet(name) { return `Hello, ${name}!`; } module.exports = greet; // Export the function
Step 2: Import the Function
In the file where you want to use the function (e.g., app.js):
// app.js const greet = require('./functions'); // Import the function console.log(greet('Alice')); // Output: Hello, Alice!
2. Using ES Modules
If your project uses ES modules (type: “module” in package.json or .mjs file extension):
Step 1: Export the Function
In the file you want to export from (e.g., functions.mjs):
// functions.mjs export function greet(name) { return `Hello, ${name}!`; }
Step 2: Import the Function
In the file where you want to use the function (e.g., app.mjs):
// app.mjs import { greet } from './functions.mjs'; // Import the function console.log(greet('Alice')); // Output: Hello, Alice!
3. Exporting and Importing Multiple Functions
CommonJS:
// functions.js function greet(name) { return `Hello, ${name}!`; } function farewell(name) { return `Goodbye, ${name}!`; } module.exports = { greet, farewell }; // Export multiple functions
// app.js const { greet, farewell } = require('./functions'); console.log(greet('Alice')); // Output: Hello, Alice! console.log(farewell('Alice')); // Output: Goodbye, Alice!
ES Modules:
// functions.mjs export function greet(name) { return `Hello, ${name}!`; } export function farewell(name) { return `Goodbye, ${name}!`; }
// app.mjs import { greet, farewell } from './functions.mjs'; console.log(greet('Alice')); // Output: Hello, Alice! console.log(farewell('Alice')); // Output: Goodbye, Alice!
4. Importing Default and Named Exports Together (ES Modules)
If a file contains both default and named exports:
// functions.mjs export default function defaultFunc() { return 'This is the default export!'; } export function namedFunc() { return 'This is a named export!'; }
// app.mjs import defaultFunc, { namedFunc } from './functions.mjs'; console.log(defaultFunc()); // Output: This is the default export! console.log(namedFunc()); // Output: This is a named export!
Summary:
In Node.js, you can include functions from other files using require (CommonJS) or import (ES modules). For CommonJS, export functions using module.exports and import them with require(‘./file’). For ES modules, use export to define exports and import to include them, ensuring type: “module” is set in package.json or using .mjs files. You can export multiple functions as an object or use named/default exports in ES modules. Always use relative paths and maintain consistent module systems. ES modules are recommended for modern applications, while CommonJS is default for older versions of Node.js.