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

vite2 + vue3 ReferenceError: __dirname is not defined #334

Closed
2 tasks done
classfang opened this issue Feb 14, 2023 · 6 comments
Closed
2 tasks done

vite2 + vue3 ReferenceError: __dirname is not defined #334

classfang opened this issue Feb 14, 2023 · 6 comments

Comments

@classfang
Copy link

Verify latest release

  • I verified that the issue exists in the latest chatgpt release

Verify webapp is working

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

Environment details

vite2 + vue3 node=18.0

Describe the Bug

vite2 + vue3 ReferenceError: __dirname is not defined

@transitive-bullshit
Copy link
Owner

I don't think this error is likely to do with the chatgpt module. Note: chatgpt should only be used from Node.js server-side.

Without more info, we won't be able to help you.

@JerryCheng1
Copy link

JerryCheng1 commented Feb 14, 2023

I don't think this error is likely to do with the chatgpt module. Note: chatgpt should only be used from Node.js server-side.

Without more info, we won't be able to help you.

the same error was encountered in other vue3 project
console output:

Uncaught (in promise) ReferenceError: __dirname is not defined
    at node_modules/gpt-3-encoder/Encoder.js (Encoder.js:5:54)
    at __require2 (chunk-IKJM6GHQ.js?v=321da443:18:50)
    at node_modules/gpt-3-encoder/index.js (index.js:1:28)
    at __require2 (chunk-IKJM6GHQ.js?v=321da443:18:50)
    at chatgpt-api.ts:1:37

This is an error in the gpt-3-encoder dependency. So, click here: https://github.com/latitudegames/GPT-3-Encoder

@transitive-bullshit
Copy link
Owner

It looks like this code is trying to be used from a non-Node.js environment according to what Vite expects. That's the only reason why __dirname wouldn't be defined.

With that being said, https://github.com/latitudegames/GPT-3-Encoder has some problems, and I'm definitely looking to replace it. It has compatibility issues like this one, only support node.js, and it's not up-to-date.

If anyone has a good alternative tokenizer (like a JS version of https://github.com/openai/tiktoken), please let me know.

@JerryCheng1
Copy link

This issue solves the error

  1. Edit PATH_OF_PROJECT/node_modules/gpt-3-encoder/Encoder.js
//const encoder = JSON.parse(fs.readFileSync(path.join(__dirname, './encoder.json')));
//const bpe_file = fs.readFileSync(path.join(__dirname, './vocab.bpe'), 'utf-8');

const encoder = require('./encoder.json')
const bpe_file = require('./vocab_bpe.json')
  1. Add vocab_bpe.json into PATH_OF_PROJECT/node_modules/gpt-3-encoder
  2. Delete node_modules/.vite

Enjoy

@JamieMcDonnell
Copy link

Hi, thanks for the suggestions, but where can I find 'vocab_bpoe.json'? It's not in the repo. Do I need to convert vocab.bpe to JSON or can I find it somewhere else? Thanks a lot.

@transitive-bullshit
Copy link
Owner

@JamieMcDonnell @JerryCheng1 @classfang try the new version https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v4.6.0

It replaces the troublesome gpt-3-encoder with a better dependency.

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

4 participants