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"
inpackage.json
. - Import and export functions using ES6 module syntax.
Leave a Reply