Working with ES6 Modules

Node.js 12 introduced support for ES6 modules, allowing the use of import and export syntax natively.

javascriptCopy code// example.js
export function greet(name) {
    return `Hello, ${name}!`;
}

// app.mjs
import { greet } from './example.js';

console.log(greet('World'));

Explanation:

  • Use the .mjs extension for ES6 modules or set "type": "module" in package.json.
  • Import and export functions using ES6 module syntax.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *