From cecba7677d2110322bd8fb4e6963d802df37a5af Mon Sep 17 00:00:00 2001 From: MemTensor AutoDev Date: Tue, 30 Jun 2026 21:38:33 +0800 Subject: [PATCH 1/2] fix(memos-local-openclaw): emit ESM from tsc to match package type:module The package declared "type": "module" but the tsconfig used "module": "CommonJS", so any `npm run build` produced dist/*.js files containing `Object.defineProperty(exports, ...)` and `exports.X = ...`. Under Node 22's ESM loader (enforced by type:module), those files crashed with `ReferenceError: exports is not defined in ES module scope`, breaking the @memtensor/memos-lite-openclaw-plugin v0.2.3 release on OpenClaw 2026.5.7. Switch the tsconfig to `module: ES2022` + `moduleResolution: Bundler` (the same pair already used by apps/memos-local-plugin/) and add a regression vitest case that guards the invariant going forward. Source under src/ is unchanged; the entry index.ts already uses ESM idioms (import.meta.url), so downgrading to CJS was never a valid alternative. Fixes #1733. --- .../tests/esm-module-format.test.ts | 100 ++++++++++++++++++ apps/memos-local-openclaw/tsconfig.json | 5 +- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 apps/memos-local-openclaw/tests/esm-module-format.test.ts diff --git a/apps/memos-local-openclaw/tests/esm-module-format.test.ts b/apps/memos-local-openclaw/tests/esm-module-format.test.ts new file mode 100644 index 000000000..6d1b64d39 --- /dev/null +++ b/apps/memos-local-openclaw/tests/esm-module-format.test.ts @@ -0,0 +1,100 @@ +/** + * Regression test for #1733: + * + * The package.json declares `"type": "module"` (ESM), but the previously + * shipped v0.2.x build emitted CommonJS-flavored `dist/*.js` files because + * tsconfig.json had `"module": "CommonJS"`. Node 22+ refused to load them: + * + * ReferenceError: exports is not defined in ES module scope + * + * This test enforces three invariants going forward: + * + * 1. `package.json` keeps `"type": "module"` (the runtime needs it for + * `import.meta.url` in index.ts). + * 2. `tsconfig.json` emits ESM (`module` ∈ ES2015..ESNext, never CommonJS). + * 3. A representative TypeScript snippet compiled with the project's + * tsconfig produces ESM `export` syntax, not CommonJS + * `Object.defineProperty(exports, ...)`. + */ + +import { describe, it, expect } from "vitest"; +import * as fs from "node:fs"; +import * as path from "node:path"; + +const PLUGIN_ROOT = path.resolve(__dirname, ".."); + +function readJsonStripComments(file: string): any { + // tsconfig allows // and /* */ comments and trailing commas; strip them + // before JSON.parse so this test stays dependency-free. + let text = fs.readFileSync(file, "utf-8"); + text = text.replace(/\/\*[\s\S]*?\*\//g, ""); + text = text.replace(/(^|[^:])\/\/[^\n]*/g, "$1"); + text = text.replace(/,(\s*[}\]])/g, "$1"); + return JSON.parse(text); +} + +describe("ESM module format (regression for #1733)", () => { + it("package.json declares type: module", () => { + const pkg = readJsonStripComments(path.join(PLUGIN_ROOT, "package.json")); + expect(pkg.type).toBe("module"); + }); + + it("tsconfig.json emits ESM, not CommonJS", () => { + const tsconfig = readJsonStripComments(path.join(PLUGIN_ROOT, "tsconfig.json")); + const moduleSetting = String(tsconfig.compilerOptions?.module ?? "").toLowerCase(); + + // Forbidden: CommonJS / Node CommonJS — those emit `exports.X = ...` + // which collides with `"type": "module"` in Node 22+ ESM mode. + expect(moduleSetting).not.toBe("commonjs"); + expect(moduleSetting).not.toBe("node"); + + // Required: any of the ESM-emitting module modes. + const esmModes = new Set([ + "es2015", + "es2020", + "es2022", + "esnext", + "node16", + "node18", + "nodenext", + "preserve", + ]); + expect(esmModes.has(moduleSetting)).toBe(true); + }); + + it("tsconfig.json sets a module resolution compatible with ESM emit", () => { + const tsconfig = readJsonStripComments(path.join(PLUGIN_ROOT, "tsconfig.json")); + const resolution = String(tsconfig.compilerOptions?.moduleResolution ?? "").toLowerCase(); + + // Required for ESM emit to work without rewriting every relative import + // path to include explicit ".js" extensions throughout src/. + const allowed = new Set(["bundler", "node16", "node18", "nodenext"]); + expect(allowed.has(resolution)).toBe(true); + }); + + it("does not publish a stale dist/index.js that uses CommonJS exports", () => { + // package.files must not include "dist" — the plugin is source-only + // published (entry is index.ts, OpenClaw loads it via tsx). + const pkg = readJsonStripComments(path.join(PLUGIN_ROOT, "package.json")); + const files: string[] = Array.isArray(pkg.files) ? pkg.files : []; + for (const f of files) { + const top = f.replace(/[/\\].*$/, ""); + expect(top.toLowerCase()).not.toBe("dist"); + } + }); + + it("compiled output does not contain CommonJS export markers", async () => { + // If a dist/ already exists in the workspace, sanity-check its top-level + // entry. We do not force a build here (the plugin is source-only); we + // simply guard against committing a stale, CJS-shaped artifact. + const distEntry = path.join(PLUGIN_ROOT, "dist", "index.js"); + if (!fs.existsSync(distEntry)) { + // Nothing to check — passes by default; source-only publishing is the + // happy path for this plugin. + return; + } + const text = fs.readFileSync(distEntry, "utf-8"); + expect(text).not.toMatch(/Object\.defineProperty\(exports,/); + expect(text).not.toMatch(/^exports\./m); + }); +}); diff --git a/apps/memos-local-openclaw/tsconfig.json b/apps/memos-local-openclaw/tsconfig.json index 53c08a1d2..aee1ff9cc 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", @@ -10,10 +10,11 @@ "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, + "allowSyntheticDefaultImports": true, "declaration": true, "declarationMap": true, "sourceMap": true, - "moduleResolution": "node" + "moduleResolution": "Bundler" }, "include": ["src"], "exclude": ["node_modules", "dist", "**/*.test.ts"] From a2b2bb6351022e437f006774566fb5a4570e4a09 Mon Sep 17 00:00:00 2001 From: "Claude Code (AutoDev)" Date: Tue, 30 Jun 2026 22:04:28 +0800 Subject: [PATCH 2/2] fix: prevent ESM/CJS conflict in memos-local-openclaw plugin (#1733) Issue #1733: @memtensor/memos-lite-openclaw-plugin v0.2.3 failed to load with "ReferenceError: exports is not defined in ES module scope" because the published tarball declared `"type": "module"` in package.json while shipping `dist/*.js` files compiled as CommonJS (`exports.xxx = ...` + `require(...)`). The current v1.0.9-beta.1 already addresses this structurally by publishing source-only (`files` lists `index.ts` + `src/` + explicit `.cjs` scripts, no `dist/`), but two latent regressions remained: 1. tsconfig.json still emitted CommonJS (`"module": "CommonJS"`), so a developer running `npm run build` locally would regenerate exactly the v0.2.3 conflict in the build output. 2. There was no regression test pinning the invariant that ESM-flavoured `package.json` must not ship bare `.js` files or a `dist/` directory. Changes: - tsconfig.json: switch `module` to `ES2022` and `moduleResolution` to `Bundler` so any local build emits ESM-flavoured `.js` matching the package's `type: "module"` declaration. - tests/esm-module-format.test.ts: 6 regression assertions covering package.json type/files/main, openclaw.extensions entry kinds, and tsconfig module kind. Confirmed the test fails when the v0.2.3 conditions are re-introduced and passes once they are not. --- .../tests/esm-module-format.test.ts | 191 +++++++++++++----- 1 file changed, 137 insertions(+), 54 deletions(-) diff --git a/apps/memos-local-openclaw/tests/esm-module-format.test.ts b/apps/memos-local-openclaw/tests/esm-module-format.test.ts index 6d1b64d39..ccc251b33 100644 --- a/apps/memos-local-openclaw/tests/esm-module-format.test.ts +++ b/apps/memos-local-openclaw/tests/esm-module-format.test.ts @@ -1,54 +1,138 @@ /** - * Regression test for #1733: + * Regression tests for issue #1733: + * "@memtensor/memos-lite-openclaw-plugin v0.2.3 fails to load: + * ReferenceError: exports is not defined in ES module scope" * - * The package.json declares `"type": "module"` (ESM), but the previously - * shipped v0.2.x build emitted CommonJS-flavored `dist/*.js` files because - * tsconfig.json had `"module": "CommonJS"`. Node 22+ refused to load them: + * Root cause in v0.2.3: + * - package.json declared `"type": "module"` (so Node treats every `.js` + * file in the package as an ES module). + * - The published tarball still shipped `dist/*.js` files compiled with + * `"module": "CommonJS"`, which contain `Object.defineProperty(exports, ...)` + * and `require(...)`. Node 22+ then refused to load them. * - * ReferenceError: exports is not defined in ES module scope - * - * This test enforces three invariants going forward: - * - * 1. `package.json` keeps `"type": "module"` (the runtime needs it for - * `import.meta.url` in index.ts). - * 2. `tsconfig.json` emits ESM (`module` ∈ ES2015..ESNext, never CommonJS). - * 3. A representative TypeScript snippet compiled with the project's - * tsconfig produces ESM `export` syntax, not CommonJS - * `Object.defineProperty(exports, ...)`. + * Permanent fix — source-only publish + ESM-flavoured tsconfig: + * 1. `package.json.files` may not ship `dist/`, `dist/**`, or any + * bare `.js`/`.mjs` file. Anything JS-shaped that ships must use + * the explicit `.cjs` extension so Node interprets it as CommonJS + * regardless of the `type: "module"` setting. + * 2. `package.json.main` (and `openclaw.extensions`) must point at a + * `.ts` source file (loaded by OpenClaw via tsx) or a `.cjs` file — + * never a bare `.js` file, which would resolve as ESM and crash on + * CommonJS-flavoured output. + * 3. `tsconfig.json` must not emit CommonJS-flavoured `.js` (`module` + * must be one of the ESM variants), and `moduleResolution` must be + * compatible with ESM emit without rewriting every relative import + * to add an explicit `.js` extension. + * 4. If a `dist/index.js` ever appears in the workspace (e.g. a stale + * local build), it must not contain CommonJS export markers. */ -import { describe, it, expect } from "vitest"; -import * as fs from "node:fs"; -import * as path from "node:path"; +import { describe, expect, it } from "vitest"; +import { readFileSync, existsSync } from "node:fs"; +import { dirname, resolve, extname, join } from "node:path"; +import { fileURLToPath } from "node:url"; -const PLUGIN_ROOT = path.resolve(__dirname, ".."); +const __filename = fileURLToPath(import.meta.url); +const pluginRoot = resolve(dirname(__filename), ".."); -function readJsonStripComments(file: string): any { +function readJsonStripComments(filePath: string): Record { // tsconfig allows // and /* */ comments and trailing commas; strip them // before JSON.parse so this test stays dependency-free. - let text = fs.readFileSync(file, "utf-8"); + let text = readFileSync(filePath, "utf-8"); text = text.replace(/\/\*[\s\S]*?\*\//g, ""); text = text.replace(/(^|[^:])\/\/[^\n]*/g, "$1"); text = text.replace(/,(\s*[}\]])/g, "$1"); return JSON.parse(text); } -describe("ESM module format (regression for #1733)", () => { - it("package.json declares type: module", () => { - const pkg = readJsonStripComments(path.join(PLUGIN_ROOT, "package.json")); - expect(pkg.type).toBe("module"); +function readPackageJson(): Record { + return readJsonStripComments(resolve(pluginRoot, "package.json")); +} + +function readTsconfig(): Record { + return readJsonStripComments(resolve(pluginRoot, "tsconfig.json")); +} + +describe("issue #1733 — ESM module format does not regress", () => { + it("package.json declares type: module (matches Node 22+ ESM-by-default expectation)", () => { + const pkg = readPackageJson(); + expect( + pkg.type, + "If `type` is missing or 'commonjs', remove the ESM-specific source features (`import.meta.url`, `createRequire(import.meta.url)`) from index.ts first.", + ).toBe("module"); + }); + + it("package.json.files does not ship a `dist` directory (would re-introduce v0.2.3 CJS/ESM conflict)", () => { + const pkg = readPackageJson(); + const files: unknown[] = Array.isArray(pkg.files) ? pkg.files : []; + const offenders = files.filter((entry) => { + if (typeof entry !== "string") return false; + const normalized = entry.replace(/\\/g, "/").replace(/^\.\//, ""); + return ( + normalized === "dist" || + normalized === "dist/" || + normalized.startsWith("dist/") || + normalized.startsWith("dist\\") + ); + }); + expect( + offenders, + `Shipping dist/* in an ESM package re-introduces issue #1733: ${offenders.join(", ")}`, + ).toEqual([]); + }); + + it("package.json.files only ships .ts source or explicit .cjs scripts (never bare .js / .mjs)", () => { + const pkg = readPackageJson(); + const files: unknown[] = Array.isArray(pkg.files) ? pkg.files : []; + const offenders = files.filter((entry) => { + if (typeof entry !== "string") return false; + const ext = extname(entry).toLowerCase(); + return ext === ".js" || ext === ".mjs"; + }); + expect( + offenders, + `A bare .js file in an ESM package gets loaded as ESM. ` + + `If it contains CommonJS (exports/require) Node will throw the v0.2.3 error. ` + + `Rename to .cjs (or .ts if loaded via tsx). Offenders: ${offenders.join(", ")}`, + ).toEqual([]); + }); + + it("package.json.main points at a TypeScript source file (loaded by OpenClaw/tsx) or an explicit .cjs script", () => { + const pkg = readPackageJson(); + const main = pkg.main; + expect(typeof main).toBe("string"); + const ext = extname(main as string).toLowerCase(); + expect( + [".ts", ".cjs"].includes(ext), + `package.json.main="${main}" must use .ts or .cjs. ` + + `A .js entry under "type": "module" is exactly what broke v0.2.3.`, + ).toBe(true); }); - it("tsconfig.json emits ESM, not CommonJS", () => { - const tsconfig = readJsonStripComments(path.join(PLUGIN_ROOT, "tsconfig.json")); - const moduleSetting = String(tsconfig.compilerOptions?.module ?? "").toLowerCase(); + it("openclaw extensions only reference .ts entries (so OpenClaw loads via tsx, not Node's ESM loader)", () => { + const pkg = readPackageJson(); + const extensions = pkg.openclaw?.extensions; + expect(Array.isArray(extensions)).toBe(true); + for (const entry of extensions as unknown[]) { + expect(typeof entry).toBe("string"); + const ext = extname(entry as string).toLowerCase(); + expect( + [".ts", ".cjs"].includes(ext), + `openclaw.extensions entry "${entry}" must be .ts or .cjs to avoid Node's strict .js→ESM resolution.`, + ).toBe(true); + } + }); - // Forbidden: CommonJS / Node CommonJS — those emit `exports.X = ...` + it("tsconfig.json emits ESM, not CommonJS (would re-create the v0.2.3 conflict on local builds)", () => { + const tsc = readTsconfig(); + const moduleSetting = String(tsc.compilerOptions?.module ?? "").toLowerCase(); + + // Forbidden: any CommonJS-flavoured emit — produces `exports.X = ...` // which collides with `"type": "module"` in Node 22+ ESM mode. expect(moduleSetting).not.toBe("commonjs"); - expect(moduleSetting).not.toBe("node"); + expect(moduleSetting).not.toBe("none"); - // Required: any of the ESM-emitting module modes. + // Required: an ESM-emitting module mode. const esmModes = new Set([ "es2015", "es2020", @@ -59,41 +143,40 @@ describe("ESM module format (regression for #1733)", () => { "nodenext", "preserve", ]); - expect(esmModes.has(moduleSetting)).toBe(true); + expect( + esmModes.has(moduleSetting), + `tsconfig "module": "${moduleSetting}" is not ESM-flavoured. ` + + `Building would emit dist/*.js with module.exports/require(), which clashes with package.json "type": "module".`, + ).toBe(true); }); it("tsconfig.json sets a module resolution compatible with ESM emit", () => { - const tsconfig = readJsonStripComments(path.join(PLUGIN_ROOT, "tsconfig.json")); - const resolution = String(tsconfig.compilerOptions?.moduleResolution ?? "").toLowerCase(); + const tsc = readTsconfig(); + const resolution = String( + tsc.compilerOptions?.moduleResolution ?? "", + ).toLowerCase(); // Required for ESM emit to work without rewriting every relative import // path to include explicit ".js" extensions throughout src/. const allowed = new Set(["bundler", "node16", "node18", "nodenext"]); - expect(allowed.has(resolution)).toBe(true); - }); - - it("does not publish a stale dist/index.js that uses CommonJS exports", () => { - // package.files must not include "dist" — the plugin is source-only - // published (entry is index.ts, OpenClaw loads it via tsx). - const pkg = readJsonStripComments(path.join(PLUGIN_ROOT, "package.json")); - const files: string[] = Array.isArray(pkg.files) ? pkg.files : []; - for (const f of files) { - const top = f.replace(/[/\\].*$/, ""); - expect(top.toLowerCase()).not.toBe("dist"); - } + expect( + allowed.has(resolution), + `tsconfig "moduleResolution": "${resolution}" is incompatible with ESM emit ` + + `(or requires explicit .js extensions in every import). ` + + `Use one of: ${[...allowed].join(", ")}.`, + ).toBe(true); }); - it("compiled output does not contain CommonJS export markers", async () => { - // If a dist/ already exists in the workspace, sanity-check its top-level - // entry. We do not force a build here (the plugin is source-only); we - // simply guard against committing a stale, CJS-shaped artifact. - const distEntry = path.join(PLUGIN_ROOT, "dist", "index.js"); - if (!fs.existsSync(distEntry)) { - // Nothing to check — passes by default; source-only publishing is the - // happy path for this plugin. + it("compiled output (if any) does not contain CommonJS export markers", () => { + // The plugin is source-only published — there is normally no dist/ at all. + // But if a developer happened to run `npm run build` locally, sanity-check + // that the output is not CJS-shaped. We do not force a build here. + const distEntry = join(pluginRoot, "dist", "index.js"); + if (!existsSync(distEntry)) { + // Source-only publish is the happy path; nothing to check. return; } - const text = fs.readFileSync(distEntry, "utf-8"); + const text = readFileSync(distEntry, "utf-8"); expect(text).not.toMatch(/Object\.defineProperty\(exports,/); expect(text).not.toMatch(/^exports\./m); });