agentic
agentic copied to clipboard
Nestjs integration
Verify latest release
- [X] I verified that the issue exists in the latest
chatgpt
release
Verify webapp is working
- [X] I verify that the ChatGPT webapp is working properly for this account.
Environment details
node 18.12.1 nestjs ^8.0.0
Describe the Bug
Hello! I'm trying to integrate into my project on nest js, but it does not support esm, how can I fix this?
NestJS doesn't require a library to be ESM, I'm running it on NestJS 9.x, what errors did you get?
I have the same problem with nestJs integration.
Error [ERR_REQUIRE_ESM]: require() of ES Module [node_modules/chatgpt/build/index.js] from [file path] not supported.
"@nestjs/core": "^8.0.0
"chatgpt": "^4.1.3"
NodeJs v18.14.0
This is my tsconfig.json
{
"compilerOptions": {
"module": "NodeNext",
"target": "ES2022",
"moduleResolution": "NodeNext",
"declaration": true,
"removeComments": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"strict": true,
"strictPropertyInitialization": false,
"keyofStringsOnly": true,
"sourceMap": false,
"outDir": "./dist",
"incremental": true,
"skipLibCheck": true,
"allowJs": false
},
"include": ["src"]
}
Also I have "type": "module"
in package.json
, maybe this is the fix, I'm working in a pure ESM environment
Unfortunately that configuration break whole project;
I have this problem with Remix right now
Try to use this solution:
export const importDynamic = new Function( 'modulePath', 'return import(modulePath)', );
and using it into your function:
const { ChatGPTAPI } = await importDynamic("chatgpt");
this.gptApi = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY as string,
});
compilerOptions: { .... moduleResolution: Node16 }
add this to your tsconfig.json and enjoy
There are also previous issues on here of Nestjs users posting their workarounds.
Sorry, but commonjs is deprecated and it's a huge pain for maintainers like myself to try and support both ESM and commonjs.
odd choice that openai would make this their "official npm" when it isn't compatible out of the box with 99% of all existing codebases.
ESM only modules are a PITA.
Update: nvm about the official part, i misread the readme (it kinda is written in a way that it makes it sound like this is there official package)