transitive-bullshit / agentic

AI agent stdlib that works with any LLM and TypeScript AI SDK.
MIT License
15.98k stars 2.11k forks source link

CommonJS import problem #367

Closed pospile closed 1 year ago

pospile commented 1 year ago

Verify latest release

Verify webapp is working

Environment details

Node 18, macOS, using latest FoalTS framework

Describe the Bug

Error [ERR_REQUIRE_ESM]: require() of ES Module

even when importing as:

const { ChatGPTUnofficialProxyAPI } = await import('chatgpt');

as per documentation.

Any idea why?

pospile commented 1 year ago

Found out solution from previous issues.

Heres what helped me:

Dynamic import: const importDynamic = new Function( 'modulePath', 'return import(modulePath)', ); and then const { ChatGPTUnofficialProxyAPI } = await importDynamic("chatgpt");

williamstein commented 1 year ago

Found out solution from previous issues.

Heres what helped me:

Dynamic import: const importDynamic = new Function( 'modulePath', 'return import(modulePath)', ); and then const { ChatGPTUnofficialProxyAPI } = await importDynamic("chatgpt");

Thanks. Your suggestion works, whereas just doing await import("chatgpt") as is suggested in the official README.md does not work and fails with:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/wstein/build/cocalc/src/data/projects/812abe34-a382-4bd1-9071-29b6f4334f03/cocalc/src/packages/node_modules/.pnpm/chatgpt@5.0.10/node_modules/chatgpt/build/index.js from /Users/wstein/build/cocalc/src/data/projects/812abe34-a382-4bd1-9071-29b6f4334f03/cocalc/src/packages/server/dist/openai/chatgpt.js not supported.
Instead change the require of index.js in /Users/wstein/build/cocalc/src/data/projects/812abe34-a382-4bd1-9071-29b6f4334f03/cocalc/src/packages/server/dist/openai/chatgpt.js to a dynamic import() which is available in all CommonJS modules.
    at /Users/wstein/build/cocalc/src/data/projects/812abe34-a382-4bd1-9071-29b6f4334f03/cocalc/src/packages/server/dist/openai/chatgpt.js:32:76
    at async Object.demo (/Users/wstein/build/cocalc/src/data/projects/812abe34-a382-4bd1-9071-29b6f4334f03/cocalc/src/packages/server/dist/openai/chatgpt.js:32:28) {
  code: 'ERR_REQUIRE_ESM'
}

I wish the README.md suggested the above workaround instead, and greatly appreciate that you did.

ktamas77 commented 1 year ago

Found out solution from previous issues.

Heres what helped me:

Dynamic import: const importDynamic = new Function( 'modulePath', 'return import(modulePath)', ); and then const { ChatGPTUnofficialProxyAPI } = await importDynamic("chatgpt");

thanks. this is the ONLY thing what actually fixed it.

T-Damer commented 1 year ago

Good solution, too bad it doesn't support types for typescript

mirari commented 11 months ago

await import("chatgpt")is not work. I found another way:

const { ChatGPTAPI } = await (eval('import("chatgpt")') as Promise<typeof import('chatgpt')>);