Import and Export Module in Node.js
×


Import and Export Module in Node.js

115

Modular programming is a fundamental practice in Node.js, allowing you to split your code into manageable, reusable parts. Node.js supports both CommonJS and ES6 module systems for importing and exporting functions, objects, or variables between files.

Why Use Modules in Node.js?

Modules help organize code into separate files, making it easier to maintain, test, and reuse. Instead of putting everything into one script, you can define utility functions, configurations, routes, etc., in different modules and import them where needed.

CommonJS Module System (Default in Node.js)

The CommonJS module system uses require() and module.exports to share functionality across files. This has been the traditional module format in Node.js.

Exporting with CommonJS

// file: utils.js
function greet(name) {
    return `Hello, ${name}!`;
}

module.exports = greet;

Importing with CommonJS

// file: app.js
const greet = require('./utils');
console.log(greet('John'));

Here, the function exported from utils.js is imported into app.js using require().

Exporting Multiple Items

// file: math.js
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;

module.exports = { add, subtract };

Using the Exported Object

// file: index.js
const math = require('./math');
console.log(math.add(5, 3));

ES6 Module System (ECMAScript Modules)

ES6 Modules use import and export syntax. This format is now supported in Node.js with .mjs files or by setting "type": "module" in package.json.

Exporting in ES6

// file: greet.mjs
export function greet(name) {
    return `Hi, ${name}`;
}

Importing in ES6

// file: main.mjs
import { greet } from './greet.mjs';
console.log(greet('Alice'));

Default Export in ES6

// file: user.mjs
export default function(name) {
    return `User: ${name}`;
}
// file: main.mjs
import user from './user.mjs';
console.log(user('David'));

Mixing Import Styles

While it’s possible to mix CommonJS and ES6 modules in some cases, it's best to stick to one format consistently to avoid compatibility issues.

Choosing Between CommonJS and ES6

  • Use CommonJS (require) for backward compatibility or older projects.
  • Use ES6 modules (import/export) for modern, cleaner syntax and when working with ES modules.

Conclusion

Understanding how to import and export modules in Node.js allows you to build cleaner, more maintainable applications. Whether you choose CommonJS or ES6 modules depends on your project requirements and environment setup, but both offer powerful ways to structure your code effectively.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat