agentic
agentic copied to clipboard
nestjs import error cjs
Verify latest release
- [X] I verified that the issue exists in the latest
chatgptrelease
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?
nestjs works well on cjs.
When I introduced it, using it would be like this. I saw similar questions on Google, but they didn't solve them
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')
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')
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;
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') } 导入建设好的文件,是可以使用的。
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
}
}
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?
await import("chatgpt")is not work for me.
I found another way:
const { ChatGPTAPI } = await (eval('import("chatgpt")') as Promise<typeof import('chatgpt')>);
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')
@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) {}
...
}
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!
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.