agentic icon indicating copy to clipboard operation
agentic copied to clipboard

Nestjs integration

Open oleguslee opened this issue 2 years ago • 7 comments

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?

oleguslee avatar Feb 07 '23 11:02 oleguslee

NestJS doesn't require a library to be ESM, I'm running it on NestJS 9.x, what errors did you get?

otakustay avatar Feb 07 '23 11:02 otakustay

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

elfillo avatar Feb 07 '23 11:02 elfillo

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

otakustay avatar Feb 07 '23 12:02 otakustay

Unfortunately that configuration break whole project;

elfillo avatar Feb 07 '23 12:02 elfillo

I have this problem with Remix right now

fourcolors avatar Feb 08 '23 06:02 fourcolors

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,
});

shishenbaiye avatar Feb 09 '23 02:02 shishenbaiye

compilerOptions: { .... moduleResolution: Node16 } add this to your tsconfig.json and enjoy

msborrow avatar Feb 13 '23 07:02 msborrow

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.

transitive-bullshit avatar Feb 14 '23 06:02 transitive-bullshit

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)

ChuckJonas avatar Mar 15 '23 19:03 ChuckJonas