Namaste Node.js - Episode 4 Summary
Importing and Exporting in Node.js
Importing and Using
To import a function from a module, use the require function. Here's an example:
// In file app.js
const greet = require('./greet');
console.log(greet('World')); // Output: Hello, World!Exporting Multiple Functions/Variables
To export multiple functions or variables, attach them to the module.exports object:
// In file utils.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
module.exports = { add, subtract };Importing Multiple Exports
To import multiple exports from a module, use destructuring:
// In file app.js
const { add, subtract } = require('./utils');
console.log(add(5, 3)); // Output: 8
console.log(subtract(5, 3)); // Output: 2How require Works
When you use require to import a module, Node.js executes the code in the module file. Only the properties of module.exports are exposed to the importing file.
Example:
// In file data.js
const secret = 'hidden';
const publicData = 'visible';
module.exports = publicData;// In file app.js
const data = require('./data');
console.log(data); // Output: visible
console.log(secret); // Error: secret is not definedIn the example above, secret is not accessible outside data.js because it was not exported using module.exports.
.mjs vs. .cjs Modules
.cjs Modules (CommonJS)
- File Extension:
.jsor.cjs - Module System: CommonJS
- Usage:
require()andmodule.exports
Example:
// In file common.js
module.exports = function() { /* ... */ };.mjs Modules (ES Modules)
- File Extension:
.mjs - Module System: ES Modules (ESM)
- Usage:
importandexport
Example:
// In file module.mjs
export function greet(name) {
return `Hello, ${name}!`;
}Importing in .mjs
// In file app.mjs
import { greet } from './module.mjs';
console.log(greet('World')); // Output: Hello, World!Differences Between .mjs and .cjs
- Syntax:
.cjsusesrequireandmodule.exports, while.mjsusesimportandexport. - Compatibility:
.mjsis the standard ES Module syntax and is compatible with modern JavaScript, while.cjsis used for legacy Node.js modules. - Use Case: Use
.mjsfor new projects or when using features of ES Modules, and.cjsfor legacy code or when using the CommonJS module system.
Summary
module.exportsis used to export functions, objects, or variables from a module.requireimports these exports into another module..cjsmodules use CommonJS syntax (requireandmodule.exports), while.mjsmodules use ES Module syntax (importandexport).- These kind of pattern used for import export in Nodejs