Node.js is a powerful runtime environment that allows developers to write server-side applications using JavaScript. One of the key features of Node.js is its module system, which allows you to organize your code into reusable modules.
Creating a Module
To create a module in Node.js, you simply need to create a new file with a .js
extension. Let’s say we want to create a module called math.js
that provides functions for performing mathematical operations.
// math.js
module.exports = {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b,
divide: (a, b) => a / b,
};
In the above code, we define an object with four functions: add
, subtract
, multiply
, and divide
. We use the module.exports
object to make these functions available to other modules that require this module.
Using a Module
To use a module in Node.js, you need to require it using the require
function. Let’s say we want to use the math.js
module in another file called app.js
.
// app.js
const math = require('./math');
console.log(math.add(5, 3)); // Output: 8
console.log(math.subtract(5, 3)); // Output: 2
console.log(math.multiply(5, 3)); // Output: 15
console.log(math.divide(5, 3)); // Output: 1.6666666666666667
In the above code, we use the require
function to import the math.js
module and assign it to a variable named math
. We can then use this variable to access the exported functions from the math.js
module.
CommonJS vs. ES Modules
Node.js uses the CommonJS module system, which is different from the ES modules syntax used in modern browsers. However, starting from Node.js version 12, experimental support for ES modules has been added, and you can use the import
and export
keywords with certain configurations.
To use ES modules in Node.js, you need to add the --experimental-modules
flag when running your application and use the .mjs
file extension for your modules.
Conclusion
Using modules in Node.js allows you to organize your code into reusable and modular pieces. By using the require
function, you can easily import and use modules in your application. Whether you choose to use the CommonJS module system or experiment with ES modules, Node.js provides flexibility in how you structure and share your code. Start leveraging the power of modules in Node.js to build efficient and maintainable applications.
#NodeJS #Modules