Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(desktop): make bundled Claude Agent SDK runnable in production
Elian Doran6dd444e16797
diff --git a/apps/desktop/scripts/build.ts b/apps/desktop/scripts/build.tsindex 82b5ff3c35..d889be4195 100644--- a/apps/desktop/scripts/build.ts+++ b/apps/desktop/scripts/build.ts@@ -18,6 +18,15 @@ async function main() {// Copy node modules dependenciesbuild.copyNodeModules([ "better-sqlite3", "bindings", "file-uri-to-path" ]);++ // The Claude Agent SDK's JavaScript is bundled into main.cjs, but at+ // query time it spawns a native `claude` binary shipped as a per-platform+ // optional dependency (@anthropic-ai/claude-agent-sdk-<platform>-<arch>).+ // That binary can't be bundled — ship the host platform's package so the+ // SDK's runtime `createRequire(import.meta.url).resolve(...)` finds it in+ // dist/node_modules. (Cross-platform release builds must ship the target's+ // package; on musl Linux the `-musl` variant is installed instead.)+ build.copyNodeModules([ `@anthropic-ai/claude-agent-sdk-${process.platform}-${process.arch}` ]);build.copy("/node_modules/ckeditor5/dist/ckeditor5-content.css", "ckeditor5-content.css");build.buildFrontend();diff --git a/scripts/build-utils.ts b/scripts/build-utils.tsindex a06395b625..ba539b0afb 100644--- a/scripts/build-utils.ts+++ b/scripts/build-utils.ts@@ -79,6 +79,17 @@ export default class BuildHelper {},define: {"process.env.NODE_ENV": JSON.stringify("production"),+ // CJS output has no `import.meta`, so esbuild rewrites+ // `import.meta.url` to `{}` (→ undefined). Bundled ESM deps+ // (e.g. @anthropic-ai/claude-agent-sdk) call+ // `createRequire(import.meta.url)` at module top level, which+ // then throws `ERR_INVALID_ARG_VALUE`. Redirect it to the+ // bundle's own file so createRequire()/`.resolve()` anchor at+ // dist/ and can still locate sibling node_modules packages.+ "import.meta.url": "__bundleImportMetaUrl",+ },+ banner: {+ js: `const __bundleImportMetaUrl = require("node:url").pathToFileURL(__filename).href;`},minify: true});