Node.js modules are self-contained code units, promoting reusability and simplifying application development. This article explores module creation, export, and import using the CommonJS format, the Node.js standard.
Key Concepts:
require
and module.exports
), prevalent in Node.js and the npm ecosystem. Other formats (AMD, ESM, System.register, UMD) exist but are not covered here.module.exports
for default exports, and understanding the nuances of module.exports
versus exports
.Node.js Module Formats (Brief Overview):
While various module formats exist in JavaScript, this guide concentrates on CommonJS, the standard for Node.js. Other formats include: AMD (Asynchronous Module Definition), ESM (ES Modules), System.register, and UMD (Universal Module Definition).
Using Built-in Modules:
Node.js offers built-in modules accessible via the require
keyword. For example, the file system module (fs
) provides functions like readdir
for listing directory contents:
const fs = require('fs'); const folderPath = '/home/jim/Desktop/'; fs.readdir(folderPath, (err, files) => { files.forEach(file => { console.log(file); }); });
CommonJS modules load synchronously, processed in their appearance order.
Creating and Exporting a Module:
Let's create a user.js
module:
const getName = () => 'Jim'; exports.getName = getName;
Then, import it into index.js
:
const user = require('./user'); console.log(`User: ${user.getName()}`);
Running node index.js
outputs "User: Jim". The exports
object makes getName
available for import. The ./
prefix in require
indicates a local file; the file extension is omitted.
Exporting Multiple Items:
Multiple methods and values can be exported:
const getName = () => 'Jim'; const getLocation = () => 'Munich'; const dateOfBirth = '12.01.1982'; exports.getName = getName; exports.getLocation = getLocation; exports.dob = dateOfBirth;
Import and use them in index.js
as needed. Note that the exported names (dob
here) can differ from the original variable names.
Alternative Export Syntax:
Exports can be defined inline:
exports.getName = () => 'Jim'; exports.getLocation = () => 'Munich'; exports.dob = '12.01.1982';
Destructuring Imports:
Destructuring allows selective imports:
const { getName, dob } = require('./user'); console.log(`${getName()} was born on ${dob}.`);
Default Exports using module.exports
:
For modules exporting a single entity, module.exports
is preferred:
const fs = require('fs'); const folderPath = '/home/jim/Desktop/'; fs.readdir(folderPath, (err, files) => { files.forEach(file => { console.log(file); }); });
module.exports
vs. exports
:
exports
is initially a reference to module.exports
. However, directly reassigning module.exports
replaces the entire export object. It's best practice to consistently use module.exports
to avoid unexpected behavior.
Conclusion:
Modules are fundamental to effective Node.js development. This article provides a solid foundation for understanding and utilizing them efficiently. For further details, consult the provided resources.
FAQs:
The provided FAQs section is already well-structured and informative. No changes needed.
The above is the detailed content of Understanding module.exports and exports in Node.js. For more information, please follow other related articles on the PHP Chinese website!