How to Enable and Use ES Modules in Node.js (with .mjs and package.json)

This guide explains what ECMAScript (ES) modules are, how to enable them in Node.js using .mjs extensions or a package.json "type":"module" setting, and provides practical code examples for importing, exporting, and running ES modules from the command line.

JavaScript
JavaScript
JavaScript
How to Enable and Use ES Modules in Node.js (with .mjs and package.json)

ECMAScript (ES) modules are the official standard format for reusable JavaScript code, using import and export statements.

Node.js version 13.2.0 and later includes built‑in support for ES modules; the following sections show how to enable and use them.

Enabling ES Modules in Node.js

By default Node.js treats JavaScript files as CommonJS modules. ES modules can be enabled in three ways:

Give the module a .mjs file extension.

Add { "type": "module" } to package.json .

Run Node with node --input-type=module --eval "<module-code>" to pass module code via STDIN.

.mjs File Extension

Using the .mjs extension tells Node.js to interpret the file as an ES module.

// month-from-date.mjs (ES Module)
const MONTHS = ['January','February','March','April','May','June',
  'July','August','September','October','November','December'];
export function monthFromDate(date) {
  if (!(date instanceof Date)) {
    date = new Date(date);
  }
  return MONTHS[date.getMonth()];
}

Another module, month.mjs, imports monthFromDate and runs as a CLI script:

// month.mjs (ES Module)
import { monthFromDate } from './month-from-date.mjs';

const dateString = process.argv[2] ?? null;

console.log(monthFromDate(dateString));

Run the script from the command line:

node ./month.mjs "2021-04-28"

The console outputs April.

package.json "type":"module"

By default Node.js treats .js files as CommonJS modules. To make .js files behave as ES modules, set the type field to module in package.json:

{
  "name": "my-app",
  "version": "1.0.0",
  "type": "module"
  // ...
}

After renaming month-from-date.mjs and month.mjs to .js and adding the type field, the code runs unchanged:

node ./month.js "2021-04-28"

The console still outputs April.

ECMAScript Modules and the Node.js Environment

Overall, ES modules and CommonJS are similar, but ES module scope does not provide CommonJS‑specific variables such as require(), exports, module.exports, __dirname, and __filename. Instead, you can use import.meta.url to obtain the absolute URL of the current module:

// An ES module at path "/usr/opt/module.mjs"
console.log(import.meta.url); // "file:///usr/opt/module.mjs"
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIJavaScriptNode.js
JavaScript
Written by

JavaScript

Provides JavaScript enthusiasts with tutorials and experience sharing on web front‑end technologies, including JavaScript, Node.js, Deno, Vue.js, React, Angular, HTML5, CSS3, and more.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.