agentic icon indicating copy to clipboard operation
agentic copied to clipboard

nestjs import error cjs

Open CooperJiang opened this issue 2 years ago • 11 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

nest import error ,Because it (the library) uses CommonJS specification, but NestJS requires importing ES modules. Is there any way to solve this?

Describe the Bug

Because it (the library) uses CommonJS specification, but NestJS requires importing ES modules. Is there any way to solve this?

CooperJiang avatar Apr 04 '23 16:04 CooperJiang

nestjs works well on cjs.

purerosefallen avatar Apr 05 '23 03:04 purerosefallen

image image

When I introduced it, using it would be like this. I saw similar questions on Google, but they didn't solve them

CooperJiang avatar Apr 05 '23 04:04 CooperJiang

Have you tried other ways of importing?

const { ChatGPTAPI } = await import('chatgpt')
  // OR
const importDynamic = new Function('modulePath', 'return import(modulePath)')
const { ChatGPTAPI } = await importDynamic('chatgpt')

zhanazhan avatar Apr 05 '23 13:04 zhanazhan

Have you tried other ways of importing?

const { ChatGPTAPI } = await import('chatgpt')
  // OR
const importDynamic = new Function('modulePath', 'return import(modulePath)')
const { ChatGPTAPI } = await importDynamic('chatgpt')
image When I await import, I will report this error

const importDynamic = new Function('modulePath', 'return import(modulePath)') const { ChatGPTAPI } = await importDynamic('chatgpt') The second method is to obtain a package of an ESM module, which is the same as this method. Await eval ('import ("chatgpt") ', but it cannot be directly structured and needs to be obtained by. default. NestJS itself is not the introduced ESM. I am confused about this point, but even more strange is that when I deploy it to the Centos server, the. default disappears here. I need to make this judgment let chatgpt = await eval('import("chatgpt")'); chatgpt = chatgpt?. default ? chatgpt.default : chatgpt;

CooperJiang avatar Apr 06 '23 02:04 CooperJiang

async function example(Message,fromid) { // To use ESM in CommonJS, you can use a dynamic import like this: const { ChatGPTAPI } = await import('./chatgpt/build/index.js') } 导入建设好的文件,是可以使用的。

jyxinye avatar Apr 15 '23 09:04 jyxinye

im doing like this, it works fine in node v18.16.0

// chatGPT.module.ts
export const ChatGPTUnofficialProvider = {
  provide: 'ChatGPTUnofficial',
  useFactory: async () => {
    // nest使用cjs,chatgpt是esm,这里使用异步导入
    const { ChatGPTUnofficialProxyAPI } = await importDynamic('chatgpt');
    // 免费 - 通过第三方代理调用web端模型,可以在web端历史记录查看
    const bot = new ChatGPTUnofficialProxyAPI({
      apiReverseProxyUrl: API_REVERSE_PROXY_URL,
      accessToken: GPT_SESSION,
      fetch: globalThis.fetch
    });

    return bot
  }
}

@Module({
  imports: [],
  controllers: [ChatGPTController],
  providers: [ChatGPTService, ChatGPTUnofficialProvider],
})

export class ChatGPTModule {}
// chatGPT.service.ts
@Injectable()
export class ChatGPTService {
  @Inject('ChatGPTUnofficial') chatGPTUnofficialProvider

 async getChatGPTUnofficialResponse(body): Promise<any> {
  let res = await this.chatGPTUnofficialProvider.sendMessage('hello');
  return res
 }
}

XueMeijing avatar Apr 27 '23 02:04 XueMeijing

The dynamic import with await works for me, but then I can't use typings properly. Is there a way to fix this within the package itself?

xtrinch avatar May 08 '23 08:05 xtrinch

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

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

mirari avatar Aug 08 '23 11:08 mirari

how is this library so broken? I am also running into this issue.

Fixed it with

const importDynamic = new Function('modulePath', 'return import(modulePath)')
const { ChatGPTAPI } = await importDynamic('chatgpt')

rgomezp avatar Aug 14 '23 05:08 rgomezp

@xtrinch

The dynamic import with await works for me, but then I can't use typings properly. Is there a way to fix this within the package itself?

You can just use proper typing by type-only import. it works with dynamic import in module file; ref) https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html example)

import type { ChatGPTAPI } from 'chatgpt';


@Injectable()
export class ExtractService {
  constructor(@Inject('CHATGPT') private readonly chatgptApi: ChatGPTAPI) {}
  ...
}

Istiopaxx avatar Sep 20 '23 13:09 Istiopaxx

how is this library so broken? I am also running into this issue.

Fixed it with

const importDynamic = new Function('modulePath', 'return import(modulePath)')
const { ChatGPTAPI } = await importDynamic('chatgpt')

Worked for me too. Thank you!

BrunoVannucci avatar Oct 03 '23 00:10 BrunoVannucci

This project is undergoing a major revamp; closing out old issues as part of the prep process.

The chatgpt package is pretty outdated at this point. I recommend that you use the openai package or the openai-fetch package instead.

transitive-bullshit avatar Jun 07 '24 05:06 transitive-bullshit