Skip to content

Commit d36ea9b

Browse files
committed
Handle lack of ESM support more gracefully in VertexAI support
1 parent bbb299d commit d36ea9b

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

packages/navie/src/services/google-vertexai-completion-service.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import CompletionService, {
1717
Usage,
1818
} from './completion-service';
1919

20-
const VertexAI = import('@langchain/google-vertexai-web');
20+
const VertexAI = importVertexAI();
2121

2222
export default class GoogleVertexAICompletionService implements CompletionService {
2323
constructor(
@@ -136,3 +136,16 @@ function errorMessage(err: unknown): string {
136136
if (isNativeError(err)) return err.cause ? errorMessage(err.cause) : err.message;
137137
return String(err);
138138
}
139+
140+
function importVertexAI(): Promise<typeof import('@langchain/google-vertexai-web')> {
141+
try {
142+
return import('@langchain/google-vertexai-web');
143+
} catch (e) {
144+
// Node throws TypeError if it doesn't support modules. In this case, reject the promise
145+
// and tell the user that VertexAI support needs ESM support in node.
146+
throw new Error(
147+
'VertexAI support requires ESM support in Node. Use newer Node or --experimental-vm-modules.',
148+
{ cause: e }
149+
);
150+
}
151+
}

0 commit comments

Comments
 (0)