agentic
agentic copied to clipboard
Error [ERR_REQUIRE_ESM]
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)
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({
});
})();
Not sure I understand. I am using ESM import { ChatGPTAPI, getOpenAIAuth } from 'chatgpt'
Try searching in your code for the word require
. You should find the problem.
I ran into this problem too, using the syntax esModule import xx from xxx
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
}
If you are using typescript and commonjs, you need to
"compilerOptions": {
"target": "es2020",
"module": "node16",
"moduleResolution": "node16"
}
``
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.