From 61899cde8c7fc19791d9d7d6a5ccabc2bb8f94dd Mon Sep 17 00:00:00 2001 From: aoife cassidy Date: Thu, 7 Nov 2024 15:57:59 +0200 Subject: [PATCH] chore(agents): explicitly unsupport CommonJS (#139) there's a weird edge-case with `tsx` where if `module` is unset in package.json but the typescript file still uses module imports, it will still run, but as CommonJS, without checking for inconsistencies. this doesn't immediately break agents, instead erroring on `job_main.js`, without throwing a useful error. we do not support and have not supported CommonJS, this PR just throws an error for this specific usecase which has thrown a few folks for a loop. for reference, compiling to JavaScript and then running with those settings throws an explicit error on Node's side before ever reaching index.js. --- .changeset/plenty-fans-kiss.md | 5 +++++ agents/src/index.ts | 13 +++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 .changeset/plenty-fans-kiss.md diff --git a/.changeset/plenty-fans-kiss.md b/.changeset/plenty-fans-kiss.md new file mode 100644 index 00000000..20347ce9 --- /dev/null +++ b/.changeset/plenty-fans-kiss.md @@ -0,0 +1,5 @@ +--- +"@livekit/agents": patch +--- + +throw an error when using CommonJS with tsx diff --git a/agents/src/index.ts b/agents/src/index.ts index 0b2e1d13..a7bb5bd2 100644 --- a/agents/src/index.ts +++ b/agents/src/index.ts @@ -15,6 +15,19 @@ import * as multimodal from './multimodal/index.js'; import * as stt from './stt/index.js'; import * as tts from './tts/index.js'; +const isCommonJS = (): boolean => { + try { + return !!require; + } catch { + return false; + } +}; +if (isCommonJS()) { + throw new ReferenceError( + '@livekit/agents cannot be used in a CommonJS environment. Please set `"type": "module"` in package.json.', + ); +} + export * from './vad.js'; export * from './plugin.js'; export * from './version.js';