Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack bundle error #69

Closed
PengBoUESTC opened this issue Dec 9, 2022 · 4 comments
Closed

webpack bundle error #69

PengBoUESTC opened this issue Dec 9, 2022 · 4 comments

Comments

@PengBoUESTC
Copy link

webpack 5.66.0 compiled successfully in 3940 ms
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: xxx/node_modules/chatgpt/build/index.js
require() of ES modules is not supported.
require() of xxx/node_modules/chatgpt/build/index.js from xxx/dist/src/modules/chat/Chat.service.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from xxx/node_modules/chatgpt/package.json.

@transitive-bullshit
Copy link
Owner

From the readme:

async function example() {
  // To use ESM in CommonJS, you can use a dynamic import
  const { ChatGPTAPI } = await import('chatgpt')

  const api = new ChatGPTAPI({
    sessionToken: process.env.SESSION_TOKEN
  })
  await api.ensureAuth()

  const response = await api.sendMessage('Hello World!')
  console.log(response)
}

(This project does not support commonjs exports because they are deprecated for all intents and purposes and it is a huge pain for maintainers to support.)

Here's a solid guide on other ways to use ESM modules like chatgpt.

@transitive-bullshit
Copy link
Owner

@PengBoUESTC ESM modules can import commonjs modules, but commonjs cannot synchronously import ESM modules.

@PengBoUESTC
Copy link
Author

@PengBoUESTC ESM modules can import commonjs modules, but commonjs cannot synchronously import ESM modules.

that's may be my fault, build tool transform import to require

cyhhao pushed a commit to cyhhao/chatgpt-api that referenced this issue Feb 8, 2023
reduce the size of docker image by deleting cache files
@rzfzr
Copy link

rzfzr commented Sep 13, 2023

@PengBoUESTC ESM modules can import commonjs modules, but commonjs cannot synchronously import ESM modules.

that's may be my fault, build tool transform import to require

I believe it was the case with "vue-cli-plugin-electron-builder": "~2.1.1"

This solved it for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants