// -- main.js --
import { greet } from "./formatters.js";
import config from "./config.js";
greet("Josh", config.locale);Understanding the module graph / notebook
Understanding the module graph
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
// -- formatters.js --
import config from "./config.js";
import { log } from "./logger.js";
export function greet(name, localeName = config.defaultLocale) {
const locale = new Intl.Locale(localeName);
if (locale.language === "en") {
log(`Hello, ${name}!`);
} else if (locale.language === "fr") {
log(`Bonjour, ${name} !`);
} else {
throw new Error(`Sorry, I don't speak ${locale}.`);
}
}// -- config.js --
export default {
locale: "en-US",
defaultLocale: "en-US",
};// -- logger.js --
export function log(message) {
console.log(`[${new Date().toISOString()}] ${message}`);
}// -- first.js --
export const x = 1;// -- second.js --
export const x = 1;// -- combined.js --
export * from "./first.js";
export * from "./second.js";
// Should "x" from this module resolve to first.js or second.js?// -- config.js --
import "./non-existent.js";
export default {
locale: "en-US",
defaultLocale: "en-US",
};// -- formatters.js --
import config from "./config.js";
import { log, missing } from "./logger.js";
// The rest of the module is unchanged.// -- config.js --
export default {
locale: "en-US",
defaultLocale: "en-US",
};
throw new Error("Configuration unavailable");// -- a.js --
import { b } from "./b.js";
export const a = 2;// -- b.js --
import { a } from "./a.js";
export const b = 1;// -- a.js --
import { b } from "./b.js";
setTimeout(() => {
console.log(b); // 1
}, 10);
export const a = 2;// -- b.js --
import { a } from "./a.js";
setTimeout(() => {
console.log(a); // 2
}, 10);
export const b = 1;// -- a.js (entry module) --
import { b } from "./b.js";
export const a = 2;// -- b.js --
import { a } from "./a.js";
console.log(a); // ReferenceError: Cannot access 'a' before initialization
export const b = 1;// -- a.js (entry module) --
import { b } from "./b.js";
console.log(b); // 1
export const a = 2;// -- b.js --
import { a } from "./a.js";
setTimeout(() => {
console.log(a); // 2
}, 10);
export const b = 1;// -- a.js (entry module) --
import { isEven } from "./b.js";
export function isOdd(n) {
return n !== 0 && isEven(n - 1);
}// -- b.js --
import { isOdd } from "./a.js";
console.log(isEven(5)); // false
export function isEven(n) {
return n === 0 || isOdd(n - 1);
}// -- config.js --
const response = await fetch("https://example.com/config.json");
if (!response.ok) {
throw new Error(`Failed to fetch configuration: ${response.status}`);
}
export default await response.json();// -- entry.js --
import "./setup.js";
import "./display.js";// -- setup.js --
import { locale } from "./settings.js";
globalThis.appLocale = locale;// -- settings.js --
export const locale = "en-US";// -- display.js --
console.log(globalThis.appLocale); // "en-US"// -- settings.js --
export const locale = await Promise.resolve("en-US");// -- display.js --
import "./setup.js";
console.log(globalThis.appLocale); // "en-US"// -- a.js --
import { resolve } from "./b.js";
resolve();// -- b.js --
const { promise, resolve } = Promise.withResolvers();
export { resolve };
await promise;// -- a.js --
await import("./b.js");// -- b.js --
import "./a.js";// -- main.js --
import config from "./config.js";
process.stdin.on("data", async (data) => {
if (data.toString().trim() !== "Hi") return;
const { greet } = await import("./formatters.js");
greet("Josh", config.locale);
});export let count = 0;
export function increment() {
count++;
}import * as counter from "./counter.js";
counter.increment();
const sameCounter = await import("./counter.js");
console.log(sameCounter === counter); // true
console.log(sameCounter.count); // 1
sameCounter.increment();
console.log(counter.count); // 2export function createCounter() {
let count = 0;
return {
increment() {
return ++count;
},
};
}// -- main.js --
import source formattersSource from "./formatters.js";
import config from "./config.js";
process.stdin.on("data", async (data) => {
if (data.toString().trim() !== "Hi") return;
const { greet } = await import(formattersSource);
greet("Josh", config.locale);
});// -- main.js --
import defer * as formatters from "./formatters.js";
import config from "./config.js";
process.stdin.on("data", (data) => {
if (data.toString().trim() !== "Hi") return;
formatters.greet("Josh", config.locale);
});