agentic icon indicating copy to clipboard operation
agentic copied to clipboard

Issue with published cjs version of package

Open minimum-devs opened this issue 2 years ago • 3 comments
trafficstars

wondering if i'm doing something wrong but

with version 1.3.0 installed like this:

{
    "dependencies": {
        "chatgpt": "^1.3.0" 
    }
}

and trying to import it like this:

let { ChatGPTAPI } = require("chatgpt");

i'm still getting this error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/task/node_modules/chatgpt/build/index.js require() of ES modules is not supported.

am i doing anything wrong?

minimum-devs avatar Dec 06 '22 12:12 minimum-devs

I'm using in my Discord bot, it works fine.

GoneTone avatar Dec 06 '22 13:12 GoneTone

Try installing [email protected], I think the ^ operator might allow higher versions aswell

DerKorb avatar Dec 06 '22 13:12 DerKorb

use rollup. you could see my project (CJS format): https://github.com/oceanlvr/ChatGPTBot. 🌟 it if possibl :D

oceanlvr avatar Dec 06 '22 16:12 oceanlvr

Maybe I should just cave and add CJS support to main...

transitive-bullshit avatar Dec 06 '22 18:12 transitive-bullshit

Would help me as well. I'm using nestjs and did not find a way to use the module except for the 1.3 version.

DerKorb avatar Dec 06 '22 20:12 DerKorb

@DerKorb i think you're right! totally missed that 🤦

i will try that and update this issue if that was the problem.

minimum-devs avatar Dec 06 '22 20:12 minimum-devs

I was able to import the esm module in my nestjs project using this guide: https://jaywolfe.dev/how-to-use-es-modules-with-older-node-js-projects-the-right-way/

DerKorb avatar Dec 06 '22 21:12 DerKorb

Use ESM in CommonJS

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

  // sessionToken is required; see below for details
  const api = new ChatGPTAPI({ sessionToken: process.env.SESSION_TOKEN })

  // ensure the API is properly authenticated
  await api.ensureAuth()

  // send a message and wait for the response
  const response = await api.sendMessage(
    'Write a python version of bubble sort. Do not include example usage.'
  )

  // response is a markdown-formatted string
  console.log(response)
}

GoneTone avatar Dec 07 '22 01:12 GoneTone

Thanks for the example + PR @GoneTone 🙏

transitive-bullshit avatar Dec 07 '22 04:12 transitive-bullshit