agentic icon indicating copy to clipboard operation
agentic copied to clipboard

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported.

Open GoneTone opened this issue 2 years ago • 17 comments

node-fetch v3 can't use require(), can downgrade to v2.6.7

GoneTone avatar Dec 05 '22 08:12 GoneTone

I just released an update which removes commonjs support: https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.1.0

I was against this idea in https://github.com/transitive-bullshit/chatgpt-api/pull/8, and I'm not surprised it ended up causing problems.

@GoneTone please let me know if v1.1.0 solves your issues. You won't be able to require('chatgpt'). Instead, you need to follow the readme and use import { ChatGPTAPI } from 'chatgpt'

transitive-bullshit avatar Dec 05 '22 09:12 transitive-bullshit

I'm using it in my Discord bot, project using require()...

GoneTone avatar Dec 05 '22 09:12 GoneTone

Hey @transitive-bullshit, great work with this package as well as all your work with React Notion.

I just upgraded to 1.1.0 and am still seeing this issue. My import is in the suggested format as well:

import { ChatGPTAPI } from 'chatgpt';

Lmk if I can provide more information. Although, I do think the easiest option is probably to go with v2 of node-fetch to widen support for this package.

FarazPatankar avatar Dec 05 '22 09:12 FarazPatankar

Ahhh my bad. Just downgraded to v2 https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.1.1

transitive-bullshit avatar Dec 05 '22 09:12 transitive-bullshit

@GoneTone can you try https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.1.2? I just re-added commonjs support along with having the downgraded node-fetch v2.

transitive-bullshit avatar Dec 05 '22 09:12 transitive-bullshit

Sorry for all the confusion and version bumps.

transitive-bullshit avatar Dec 05 '22 09:12 transitive-bullshit

@GoneTone can you try https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.1.2? I just re-added commonjs support along with having the downgraded node-fetch v2.

it's not working

14Kay avatar Dec 05 '22 09:12 14Kay

@14kk can you please give me a more detailed error you're seeing and/or code to reproduce the error?

transitive-bullshit avatar Dec 05 '22 09:12 transitive-bullshit

remark

Error [ERR_REQUIRE_ESM]: require() of ES Module E:\...\node_modules\remark\index.js from E:\...\node_modules\chatgpt\build\index.js not supported.

GoneTone avatar Dec 05 '22 09:12 GoneTone

encounter the same problem.

"chatgpt": "^1.1.2", "node-fetch": "^2.6.7",

happybit avatar Dec 05 '22 09:12 happybit

Okay; for now, I'm going to disable commonjs support because it is not working via https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.1.3

If you are currently using require('chatgpt'), you'll need to upgrade to the latest 1.1.3 and follow this guide in order to use the ESM version: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

I'm sorry if this is an inconvenience for you, but it was my original intention to only support ESM as many other packages in the NPM ecosystem are increasingly doing.

If you are still having an issue using 1.1.3 or later and are using the ESM version via import { ChatGPTAPI } from 'chatgpt', please let me know with as much detail as possible — including a link to your code so I can try to reproduce any issues. I'm using the ESM version in https://github.com/transitive-bullshit/chatgpt-twitter-bot and have verified that the version on NPM is working as expected.

transitive-bullshit avatar Dec 05 '22 10:12 transitive-bullshit

@transitive-bullshit I am on 1.1.3 and still seeing an error when I try to start my app. I believe it has something to do with the type being set to module here: https://github.com/transitive-bullshit/chatgpt-api/blob/main/package.json#L8

You can view the output here: https://app.warp.dev/block/cJcagd7Gq7bwEldsZLmYpR

FarazPatankar avatar Dec 05 '22 10:12 FarazPatankar

@FarazPatankar your code is being transpiled / loaded as commonjs.

transitive-bullshit avatar Dec 05 '22 10:12 transitive-bullshit

@transitive-bullshit I successfully modified chatgpt-api to support cjs, wait I will open a pull request!

GoneTone avatar Dec 05 '22 10:12 GoneTone

@transitive-bullshit I successfully modified chatgpt-api to support cjs, wait I will open a pull request!

can't wait for it

happybit avatar Dec 05 '22 10:12 happybit

@transitive-bullshit I successfully modified chatgpt-api to support cjs, wait I will open a pull request!

can't wait for it

+1

14Kay avatar Dec 05 '22 10:12 14Kay

https://github.com/transitive-bullshit/chatgpt-api/pull/19

GoneTone avatar Dec 05 '22 11:12 GoneTone

Please use v1.3.0 which includes support for CommonJS via #19.

Thanks @GoneTone 🙏

If you run into an issue with v1.3.0 and commonjs, please open a separate issue to discuss and provide as many details about your environment as possible — including a minimal reproduction if possible.

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

I use chatgpt-api package in my project which is based on typescript. When I'm going to run my code with ts-node index.ts, the error encountered:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/node_modules/chatgpt/build/index.js from /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/chatgpt.ts not supported.
Instead change the require of index.js in /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/chatgpt.ts to a dynamic import() which is available in all CommonJS modules.

My code used ESM like

import { ChatGPTAPI, ChatGPTConversation } from 'chatgpt';
import { SESSION_TOKEN } from './configs';
import log4js from './logger';

TankNee avatar Dec 08 '22 03:12 TankNee

I use chatgpt-api package in my project which is based on typescript. When I'm going to run my code with ts-node index.ts, the error encountered:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/node_modules/chatgpt/build/index.js from /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/chatgpt.ts not supported.
Instead change the require of index.js in /Users/tanknee/Documents/LocalCodeRepo/chatgpt-padlocal/chatgpt.ts to a dynamic import() which is available in all CommonJS modules.

My code used ESM like

import { ChatGPTAPI, ChatGPTConversation } from 'chatgpt';
import { SESSION_TOKEN } from './configs';
import log4js from './logger';

When I use tsx instead of ts-node, the code works.

TankNee avatar Dec 08 '22 06:12 TankNee

it is like ES module dictatorship.

is it possible to use it with regular require?

askucher avatar Feb 10 '23 16:02 askucher

I was able to fix this in typescript(express js) by installing tsx in devDependencies and changing my scripts to "scripts": { "dev": "tsx watch src/index.ts", "build": "tsc", "start": "tsx dist/index.js" } and installing node >= 18

tribeless avatar Mar 12 '23 16:03 tribeless

I have this problem to this day, how do I solve it?

[1] Instead change the require of index.js in ... to a dynamic import() which is available in all CommonJS modules.```

4skinSkywalker avatar Nov 11 '23 15:11 4skinSkywalker