Skip to content

Commit

Permalink
chore(agents): explicitly unsupport CommonJS (#139)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nbsp authored Nov 7, 2024
1 parent d9273f2 commit 61899cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-fans-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@livekit/agents": patch
---

throw an error when using CommonJS with tsx
13 changes: 13 additions & 0 deletions agents/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 61899cd

Please sign in to comment.