Modules and Imports
Last Updated: Apr 13, 2025
Exporting in CommonJS
Create a file called math.js
:
function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } // Export functions module.exports = { add, subtract };
Importing in CommonJS
In app.js
, import the module using require()
:
const math = require("./math"); console.log(math.add(5, 3)); // Outputs: 8 console.log(math.subtract(10, 4)); // Outputs: 6
Exporting a Single Value
module.exports = function (name) { return `Hello, ${name}!`; };
Importing a single exported function:
const greet = require("./greet"); console.log(greet("Alice")); // Outputs: Hello, Alice!
ES6 Modules (ESM)
ES6 Modules are the modern way to work with modules in JavaScript. They use export
and import
and work in both browsers and Node.js (with "type": "module"
in package.json
).
Named Exports
Create math.js
:
export function add(a, b) { return a + b; } export function subtract(a, b) { return a - b; }
Default Exports
Only one default export per file:
export default function multiply(a, b) { return a * b; }
Importing in ES6 Modules
Importing Named Exports
In app.js
:
import { add, subtract } from "./math.js"; console.log(add(5, 3)); // Outputs: 8 console.log(subtract(10, 4)); // Outputs: 6
Importing a Default Export
import multiply from "./math.js"; console.log(multiply(4, 5)); // Outputs: 20
Importing Everything from a Module
import * as math from "./math.js"; console.log(math.add(2, 3)); console.log(math.subtract(7, 2));
Using ES6 Modules in Node.js
To use ES6 modules in Node.js, add "type": "module"
in package.json
:
Then, use import
instead of require()
.
import { add } from "./math.js"; console.log(add(10, 5));
Differences Between CommonJS and ES6 Modules
FeatureCommonJS (require
)ES6 Modules (import
)Default inNode.jsModern JavaScript (ES6)Syntaxrequire()
/ module.exportsimport
/ export
Load TypeSynchronousAsynchronousCan be used in BrowsersNo (without bundlers)Yes (Supported in modern browsers)
When to Use Which?
Use CommonJS for Node.js applications that do not require modern JavaScript syntax.
Use ES6 Modules for modern JavaScript projects, browsers, and future-proof development.
Conclusion
Modules improve code reusability and organization. ES6 Modules (import/export
) are the standard for modern JavaScript, while CommonJS (require/module.exports
) is still widely used in Node.js. The next section will cover working with databases in JavaScript, focusing on connecting Node.js to relational and NoSQL databases.
Modules and Imports
Last Updated: Apr 13, 2025
Exporting in CommonJS
Create a file called math.js
:
function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } // Export functions module.exports = { add, subtract };
Importing in CommonJS
In app.js
, import the module using require()
:
const math = require("./math"); console.log(math.add(5, 3)); // Outputs: 8 console.log(math.subtract(10, 4)); // Outputs: 6
Exporting a Single Value
module.exports = function (name) { return `Hello, ${name}!`; };
Importing a single exported function:
const greet = require("./greet"); console.log(greet("Alice")); // Outputs: Hello, Alice!
ES6 Modules (ESM)
ES6 Modules are the modern way to work with modules in JavaScript. They use export
and import
and work in both browsers and Node.js (with "type": "module"
in package.json
).
Named Exports
Create math.js
:
export function add(a, b) { return a + b; } export function subtract(a, b) { return a - b; }
Default Exports
Only one default export per file:
export default function multiply(a, b) { return a * b; }
Importing in ES6 Modules
Importing Named Exports
In app.js
:
import { add, subtract } from "./math.js"; console.log(add(5, 3)); // Outputs: 8 console.log(subtract(10, 4)); // Outputs: 6
Importing a Default Export
import multiply from "./math.js"; console.log(multiply(4, 5)); // Outputs: 20
Importing Everything from a Module
import * as math from "./math.js"; console.log(math.add(2, 3)); console.log(math.subtract(7, 2));
Using ES6 Modules in Node.js
To use ES6 modules in Node.js, add "type": "module"
in package.json
:
Then, use import
instead of require()
.
import { add } from "./math.js"; console.log(add(10, 5));
Differences Between CommonJS and ES6 Modules
FeatureCommonJS (require
)ES6 Modules (import
)Default inNode.jsModern JavaScript (ES6)Syntaxrequire()
/ module.exportsimport
/ export
Load TypeSynchronousAsynchronousCan be used in BrowsersNo (without bundlers)Yes (Supported in modern browsers)
When to Use Which?
Use CommonJS for Node.js applications that do not require modern JavaScript syntax.
Use ES6 Modules for modern JavaScript projects, browsers, and future-proof development.
Conclusion
Modules improve code reusability and organization. ES6 Modules (import/export
) are the standard for modern JavaScript, while CommonJS (require/module.exports
) is still widely used in Node.js. The next section will cover working with databases in JavaScript, focusing on connecting Node.js to relational and NoSQL databases.
Modules and Imports
Last Updated: Apr 13, 2025
Exporting in CommonJS
Create a file called math.js
:
function add(a, b) { return a + b; } function subtract(a, b) { return a - b; } // Export functions module.exports = { add, subtract };
Importing in CommonJS
In app.js
, import the module using require()
:
const math = require("./math"); console.log(math.add(5, 3)); // Outputs: 8 console.log(math.subtract(10, 4)); // Outputs: 6
Exporting a Single Value
module.exports = function (name) { return `Hello, ${name}!`; };
Importing a single exported function:
const greet = require("./greet"); console.log(greet("Alice")); // Outputs: Hello, Alice!
ES6 Modules (ESM)
ES6 Modules are the modern way to work with modules in JavaScript. They use export
and import
and work in both browsers and Node.js (with "type": "module"
in package.json
).
Named Exports
Create math.js
:
export function add(a, b) { return a + b; } export function subtract(a, b) { return a - b; }
Default Exports
Only one default export per file:
export default function multiply(a, b) { return a * b; }
Importing in ES6 Modules
Importing Named Exports
In app.js
:
import { add, subtract } from "./math.js"; console.log(add(5, 3)); // Outputs: 8 console.log(subtract(10, 4)); // Outputs: 6
Importing a Default Export
import multiply from "./math.js"; console.log(multiply(4, 5)); // Outputs: 20
Importing Everything from a Module
import * as math from "./math.js"; console.log(math.add(2, 3)); console.log(math.subtract(7, 2));
Using ES6 Modules in Node.js
To use ES6 modules in Node.js, add "type": "module"
in package.json
:
Then, use import
instead of require()
.
import { add } from "./math.js"; console.log(add(10, 5));
Differences Between CommonJS and ES6 Modules
FeatureCommonJS (require
)ES6 Modules (import
)Default inNode.jsModern JavaScript (ES6)Syntaxrequire()
/ module.exportsimport
/ export
Load TypeSynchronousAsynchronousCan be used in BrowsersNo (without bundlers)Yes (Supported in modern browsers)
When to Use Which?
Use CommonJS for Node.js applications that do not require modern JavaScript syntax.
Use ES6 Modules for modern JavaScript projects, browsers, and future-proof development.
Conclusion
Modules improve code reusability and organization. ES6 Modules (import/export
) are the standard for modern JavaScript, while CommonJS (require/module.exports
) is still widely used in Node.js. The next section will cover working with databases in JavaScript, focusing on connecting Node.js to relational and NoSQL databases.
ECOSYSTEMS
FOLLOW US
Copyright ©2025. Open Ecosystems
ECOSYSTEMS
FOLLOW US
Copyright ©2025. Open Ecosystems
ECOSYSTEMS
FOLLOW US
Copyright ©2025. Open Ecosystems