"use strict"; var _a = _interopRequireDefault(require("./lib.mjs")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } console.log((0, _a.default)(2)); app.cjs export default function(x) { return x * x; } lib.mjs $ node app.cjs node:internal/modules/cjs/loader:1031 throw new ERR_REQUIRE_ESM(filename, true); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module lib.mjs not supported. Instead change the require of lib.mjs to a dynamic import() which is available in all CommonJS modules.
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; function _default(x) { return x * x; } lib.cjs import square from "./lib.cjs"; console.log(square(2)); app.mjs $ node app.mjs app.mjs:3 console.log(square(2)); ^ TypeError: square is not a function at app.mjs:3:13
Inc. const { value } = require("./sleep-lib.mjs"); console.log("value =", value); sleep-app.cjs Discrepancy in asynchrony Error! $ node sleep-app.cjs node:internal/modules/cjs/loader:1031 throw new ERR_REQUIRE_ESM(filename, true); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module sleep-lib.mjs not supported.
const { value } = await import("./sleep-lib.mjs"); console.log("value =", value); sleep-app.cjs Discrepancy in asynchrony Error! $ node sleep-app.cjs sleep-app.cjs:1 const { value } = await import("./sleep-lib.mjs"); ^^^^^ SyntaxError: await is only valid in async functions and the top level bodies of modules
import("./sleep-lib.mjs").then(({ value }) => { console.log("value =", value); exports.value2 = value * 2; }); sleep-app.cjs Discrepancy in asynchrony Too late!
import * as ns from ""; ✅ Namespace ❌ Default ✅ Named import f from ""; import { f } from ""; const ns = require(""); const f = require(""); const { f } = require(""); = ∈
import * as ns from ""; ✅ Namespace ❌ Default ✅ Named import { default as f } from ""; import { f } from ""; const ns = require(""); const f = require(""); const { f } = require(""); = ∈
Inc. import * as ns from ""; import f from ""; import { f } from ""; const ns = require(""); const f = require(""); const { f } = require(""); = ∈ (except “default”) (except “default”)
Inc. import * as ns from ""; import f from ""; import { f } from ""; const ns = require(""); const f = require(""); const { f } = require(""); = ∈ (Loss of information)
Wantedly, Inc. console.log("Initializing..."); export const foo = 1; lib.mjs import { bar } from "./lib.mjs"; console.log(bar); app.mjs $ node app.mjs app.mjs:1 import { bar } from "./lib.mjs"; ^^^ SyntaxError: The requested module './lib.mjs' does not provide an export named 'bar'
Wantedly, Inc. console.log("Initializing..."); export const foo = 1; lib.mjs import { bar } from "./lib.mjs"; console.log(bar); app.mjs $ node app.mjs app.mjs:1 import { bar } from "./lib.mjs"; ^^^ SyntaxError: The requested module './lib.mjs' does not provide an export named 'bar' No “Initializing…” printed
Wantedly, Inc. console.log("Initializing..."); exports["ba" + "r"] = 2; lib.cjs import { bar } from "./lib.cjs"; console.log(bar); app.mjs $ node app.mjs app.mjs:1 import { bar } from "./lib.cjs"; ^^^ SyntaxError: Named export 'bar' not found. The requested module './lib.cjs' is a CommonJS module, which may not support all module.exports as named exports.
2022 Wantedly, Inc. console.log("imported"); lib/main.js import "./lib"; app.mjs { "main": "./main.js" } lib/package.json $ node app.mjs node:internal/process/esm_loader:97 internalBinding('errors').triggerUncaughtExcep tion( ^ Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'lib' is not supported resolving ES modules imported from app.mjs Did you mean to import ../lib/main.js?
Inc. console.log(`I'm ${__filename}`); export {}; meta.mjs $ node meta.mjs file:///<dir>/meta.mjs:1 console.log(`I'm ${__filename}`); ^ ReferenceError: __filename is not defined in ES module scope
"esm-test"; import "./app.cjs"; app.mjs require("esm-test"); app.cjs package.json lib.mjs lib.cjs $ node app.mjs ESM entrypoint CJS entrypoint Imagine this is state initialization. There are two different states now!
"node:fs"; import vm from "node:vm"; const m = new vm.SourceTextModule( fs.readFileSync("lib.mjs", "utf-8")); await m.link(() => {}); await m.evaluate(); console.log(m.namespace.default); app.mjs
42; lib.mjs $ node --experimental-vm-modules app.cjs 42 (node:10167) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
package.json to use Module type detection type Nearest ancestor of the resolved module Package exports exports main `segment` or `@scope/segment` part of the request Folder main (only in CJS/require) main The requested directory Package imports imports Nearest ancestor of the requesting module Self name exports Nearest ancestor of the requesting module