From c9fee5cf3bb971cf46abbde569a1b9290762b162 Mon Sep 17 00:00:00 2001 From: MemOS AutoDev Date: Thu, 2 Jul 2026 20:23:49 +0800 Subject: [PATCH] fix(memos-local-openclaw): align tsconfig with package.json "type": "module" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #1733 reported that the legacy @memtensor/memos-lite-openclaw-plugin v0.2.3 failed to load on Node 22 / OpenClaw 2026.5.7 with "ReferenceError: exports is not defined in ES module scope". Root cause: tsc emitted CommonJS (exports.initPlugin = ...) into dist/, while package.json declared "type": "module", so Node refused to evaluate the compiled entry as ESM. The same latent misalignment lived in the successor plugin at apps/memos-local-openclaw: tsconfig.json had "module": "CommonJS" + "moduleResolution": "node" while package.json still declares "type": "module". It doesn't manifest today because "files" excludes dist/ and the plugin loads index.ts directly via tsx, but any accidental inclusion of dist/ (as v0.2.3 did) would reproduce the same crash. The source is already ESM-native — index.ts uses import.meta.url and createRequire(import.meta.url). Changes: - tsconfig.json: module "CommonJS" -> "ES2022"; moduleResolution "node" -> "bundler". Now consistent with sibling apps/memos-local-plugin and with package.json "type": "module". - tests/module-format.test.ts: new regression guard asserting the invariant "package.json type=module implies tsconfig emits ESM + moduleResolution is ESM-safe + no .cjs entry point sneaks into main/openclaw.extensions". Verified to fail (with the exact v0.2.3 diagnostic) when tsconfig is reverted to the old CommonJS setup. Verification: - vitest run tests/module-format.test.ts -> 4/4 passed - tsc --noEmit -> clean - tsc build -> dist/*.js emit ESM (import/export), no exports./module.exports - Sanity: chunker/config/storage/plugin-openclaw-wiring/plugin-impl-access tests (32 total) still pass unchanged. Refs: #1733 --- .../tests/module-format.test.ts | 97 +++++++++++++++++++ apps/memos-local-openclaw/tsconfig.json | 4 +- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 apps/memos-local-openclaw/tests/module-format.test.ts diff --git a/apps/memos-local-openclaw/tests/module-format.test.ts b/apps/memos-local-openclaw/tests/module-format.test.ts new file mode 100644 index 000000000..2169c5352 --- /dev/null +++ b/apps/memos-local-openclaw/tests/module-format.test.ts @@ -0,0 +1,97 @@ +/** + * Regression test for issue #1733 + * (@memtensor/memos-lite-openclaw-plugin v0.2.3 fails to load: + * "ReferenceError: exports is not defined in ES module scope"). + * + * Root cause: package.json declared `"type": "module"` while tsconfig.json + * emitted CommonJS (`"module": "CommonJS"`). Node.js treated the compiled + * `dist/index.js` as ESM, saw the `exports.` writes, and refused to load + * the plugin. + * + * Guard the invariant so it cannot silently drift again: + * - When package.json says "type": "module", tsconfig must emit an ESM + * module format (not CommonJS / node16 / commonjs variants). + * - When ESM is chosen, `moduleResolution` must be one of the ESM-safe + * variants ("bundler" / "nodenext" / "node16"). Bare `"node"` (which is + * equivalent to the legacy classic CJS resolver) would silently mask + * ESM-only imports at build time. + */ + +import { readFileSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +import { describe, expect, it } from "vitest"; + +// Strip // and /* */ comments and trailing commas so JSON.parse can eat tsconfig. +function readJsoncSync(path: string): any { + const raw = readFileSync(path, "utf-8"); + const noBlock = raw.replace(/\/\*[\s\S]*?\*\//g, ""); + const noLine = noBlock.replace(/(^|[^:])\/\/.*$/gm, "$1"); + const noTrailingCommas = noLine.replace(/,(\s*[}\]])/g, "$1"); + return JSON.parse(noTrailingCommas); +} + +const pluginRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); +const pkgJsonPath = resolve(pluginRoot, "package.json"); +const tsconfigPath = resolve(pluginRoot, "tsconfig.json"); + +const CJS_MODULES = new Set(["commonjs", "node16-commonjs"]); +const ESM_MODULES = new Set([ + "es2015", + "es2020", + "es2022", + "esnext", + "node16", + "node18", + "nodenext", +]); +const ESM_SAFE_RESOLUTIONS = new Set([ + "bundler", + "node16", + "node18", + "nodenext", +]); + +describe("regression #1733 — plugin module-format alignment", () => { + const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8")); + const tsconfig = readJsoncSync(tsconfigPath); + const module = String(tsconfig.compilerOptions?.module ?? "").toLowerCase(); + const moduleResolution = String( + tsconfig.compilerOptions?.moduleResolution ?? "", + ).toLowerCase(); + + it("package.json declares ESM", () => { + // If this ever flips back to CommonJS or is removed, the tsconfig side + // of the invariant needs to be revisited in tandem. + expect(pkg.type).toBe("module"); + }); + + it("tsconfig emits an ESM-compatible module format", () => { + expect(ESM_MODULES.has(module), `tsconfig module=${module} is CJS-flavoured; would recreate the v0.2.3 "exports is not defined" crash when dist/index.js is loaded under package.json "type": "module".`).toBe(true); + expect(CJS_MODULES.has(module)).toBe(false); + }); + + it("tsconfig moduleResolution is ESM-safe", () => { + expect( + ESM_SAFE_RESOLUTIONS.has(moduleResolution), + `moduleResolution="${moduleResolution}" pairs the ESM emit with the legacy CJS resolver — imports get silently rewritten in ways Node can't execute at runtime.`, + ).toBe(true); + }); + + it("no legacy CommonJS entry point sneaks back in", () => { + // main + openclaw.extensions must not reference a CJS-only artifact. + const main: string = pkg.main ?? ""; + expect(main.endsWith(".cjs")).toBe(false); + + const ext = pkg.openclaw?.extensions; + if (Array.isArray(ext)) { + for (const e of ext) { + expect( + String(e).endsWith(".cjs"), + `openclaw.extensions entry "${e}" would force CommonJS load under type:module.`, + ).toBe(false); + } + } + }); +}); diff --git a/apps/memos-local-openclaw/tsconfig.json b/apps/memos-local-openclaw/tsconfig.json index 53c08a1d2..e4f2cd2b9 100644 --- a/apps/memos-local-openclaw/tsconfig.json +++ b/apps/memos-local-openclaw/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "ES2022", - "module": "CommonJS", + "module": "ES2022", "lib": ["ES2022"], "outDir": "dist", "rootDir": "src", @@ -13,7 +13,7 @@ "declaration": true, "declarationMap": true, "sourceMap": true, - "moduleResolution": "node" + "moduleResolution": "bundler" }, "include": ["src"], "exclude": ["node_modules", "dist", "**/*.test.ts"]