agentic icon indicating copy to clipboard operation
agentic copied to clipboard

Error [ERR_REQUIRE_ESM]

Open shivamsinha15 opened this issue 2 years ago • 6 comments

Getting the following error during: import { ChatGPTAPI, getOpenAIAuth } from 'chatgpt'

Error [ERR_REQUIRE_ESM]: require() of ES Module /init-nodejs-project/node_modules/chatgpt/build/index.js from /init-nodejs-project/src/service/TestService.js not supported.
Instead change the require of index.js in /init-nodejs-project/src/service/TestService.js to a dynamic import() which is available in all CommonJS modules.
    at Object.newLoader [as .js] (/init-nodejs-project/node_modules/pirates/lib/index.js:141:7)

shivamsinha15 avatar Dec 13 '22 03:12 shivamsinha15

https://github.com/transitive-bullshit/chatgpt-api/blob/main/readme.md#compatibility

look here you must use es module,if you want use in commonjs,you must

(async () => {
    const { ChatGPTAPI } = await import('chatgpt');

    const api = new ChatGPTAPI({

    });

   
})();

Zephylaci avatar Dec 13 '22 03:12 Zephylaci

Not sure I understand. I am using ESM import { ChatGPTAPI, getOpenAIAuth } from 'chatgpt'

shivamsinha15 avatar Dec 13 '22 04:12 shivamsinha15

Try searching in your code for the word require. You should find the problem.

MarvynSTAR avatar Dec 13 '22 06:12 MarvynSTAR

I ran into this problem too, using the syntax esModule import xx from xxx

wy2022 avatar Dec 13 '22 07:12 wy2022

If you using some compiler like swc, add ignoreDynamic in module prevent it compile to require. And maybe webpack use /* webpackIgnore:true */ in import or some plugin can help.

"module": {
    "type": "commonjs",
    "ignoreDynamic": true
}

spd789562 avatar Dec 13 '22 08:12 spd789562

If you are using typescript and commonjs, you need to

"compilerOptions": {
    "target": "es2020",
    "module": "node16",
    "moduleResolution": "node16"
}
``

saltcute avatar Dec 13 '22 17:12 saltcute

Thanks everyone who's chimed in w/ help.

If you need help getting ES modules to work and aren't familiar with working with them, it's very likely it's not an issue with this package, but rather an issue with your project's webpack / tsconfig / imports.

Please search existing issues first, and only if you really can't find a solution online (e.g., stackoverflow), then you can open an issue with as much detail as possible about how you're using the package, including a minimal reproduction repo if at all possible. I will not look at commonjs / ESM issues without a minimal reproduction scenario because I just don't have the bandwidth and unfortunately most of these issues have more to do with your app's config than this package.

transitive-bullshit avatar Dec 13 '22 19:12 transitive-bullshit