// Using dynamic import to load the math module
console.log("dynamically importing ./math.mjs");
import('./math.mjs')
.then((mathModule) => {
// Now you can use the functions from the loaded module
const result1 = mathModule.add(5, 3);
const result2 = mathModule.subtract(10, 4);
sh-3.2$ cat dynamic_import.js // dynamicImportExample.js
// Using dynamic import to load the math module console.log("dynamically importing ./math.mjs"); import('./math.mjs') .then((mathModule) => { // Now you can use the functions from the loaded module const result1 = mathModule.add(5, 3); const result2 = mathModule.subtract(10, 4);
}) .catch((error) => { console.error('Dynamic import error:', error); });
sh-3.2$ node dynamic_import.js dynamically importing ./math.mjs Addition result: 5, 3 -> 8 Subtraction result: 10, 4 -> 6