agentic icon indicating copy to clipboard operation
agentic copied to clipboard

Issue importing from nodejs / typescript backend

Open sfakir opened this issue 1 year ago • 7 comments

Verify latest release

  • [X] I verified that the issue exists in the latest chatgpt release

Verify webapp is working

  • [X] I verify that the ChatGPT webapp is working properly for this account.

Environment details

  • Node version 18
  • NestJS Typescript

Describe the Bug

Importing the chatGPT module does not work. It thoughts the error below.

reproducable:

  • import { ChatGPTAPI } from 'chatgpt';
  • const chatgptAPI = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY });

Error:

@nx/js/src/executors/node/node-with-require-overrides.js:18 return originalLoader.apply(this, arguments);

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/chatgpt/build/index.js from brain-api/main.js not supported. Instead change the require of index.js in apps/brain-api/main.js to a dynamic import() which is available in all CommonJS modules. at Module._load (node_modules/@nx/js/src/executors/node/node-with-require-overrides.js:18:31) at Object.defineProperty.value (react/dist/apps/brain-api/main.js:794:18) at webpack_require (dist/apps/brain-api/ma ...

sfakir avatar Aug 21 '23 15:08 sfakir

I also get this error

Vagif12 avatar Aug 24 '23 13:08 Vagif12

Try set "type": "module" to your package.json?

cctv1005s avatar Sep 04 '23 06:09 cctv1005s

Still doesn't work @cctv1005s . This occurs with me when trying to use probot Library

Vagif12 avatar Sep 04 '23 06:09 Vagif12

very annoying :( same problem, if i change type to module i get:

module.exports = require("@nestjs/core"); ^ ReferenceError: require is not defined in ES module scope, you can use import instead This file is being treated as an ES module because it has a '.js' file extension ...

How do we fix it?

mmaestrini avatar Sep 27 '23 14:09 mmaestrini

Maybe its export in package.json https://github.com/meteor/meteor/discussions/11727#discussioncomment-2199815

SpiritAnri avatar Oct 06 '23 10:10 SpiritAnri

I got the error too. same within NestJs backend. has anyone got a resolution?

swellee avatar Feb 20 '24 08:02 swellee

This has been a way to make it work on Electron's backend:

import './fetch-polyfill.js'
import { log } from './logger'
const { ChatGPTAPI } = await import(/* webpackIgnore: true */'chatgpt')
const api = new ChatGPTAPI({ apiKey: 'sk-...

but it only works in development, on a built project I get this:

Cannot find package 'chatgpt' imported from C:\Users\...\background.js
Did you mean to import chatgpt/build/index.js?

Any ideas?

rzfzr avatar Mar 23 '24 15:03 rzfzr

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.

transitive-bullshit avatar Jun 07 '24 05:06 transitive-bullshit