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

Issues importing the package #1

Closed
Tymek opened this issue Feb 1, 2022 · 4 comments · Fixed by #2
Closed

Issues importing the package #1

Tymek opened this issue Feb 1, 2022 · 4 comments · Fixed by #2

Comments

@Tymek
Copy link

Tymek commented Feb 1, 2022

No matching export in "browser-external:util" for import "TextEncoder" in Vite (SvelteKit).

 > node_modules/gpt3-tokenizer/dist/gpt3-tokenizer.esm.js:1:9: error: No matching export in "browser-external:util" for import "TextEncoder"
    1 │ import { TextEncoder, TextDecoder } from 'util';
      ╵          ~~~~~~~~~~~

 > node_modules/gpt3-tokenizer/dist/gpt3-tokenizer.esm.js:1:22: error: No matching export in "browser-external:util" for import "TextDecoder"
    1 │ import { TextEncoder, TextDecoder } from 'util';
      ╵                       ~~~~~~~~~~~

12:57:34 AM [vite] error while updating dependencies:
Error: Build failed with 2 errors:
node_modules/gpt3-tokenizer/dist/gpt3-tokenizer.esm.js:1:9: error: No matching export in "browser-external:util" for import "TextEncoder"
node_modules/gpt3-tokenizer/dist/gpt3-tokenizer.esm.js:1:22: error: No matching export in "browser-external:util" for import "TextDecoder"
    at failureErrorWithLog (/src/tymek-cz/node_modules/esbuild/lib/main.js:1493:15)
    at /src/tymek-cz/node_modules/esbuild/lib/main.js:1151:28
    at runOnEndCallbacks (/src/tymek-cz/node_modules/esbuild/lib/main.js:941:63)
    at buildResponseToResult (/src/tymek-cz/node_modules/esbuild/lib/main.js:1149:7)
    at /src/tymek-cz/node_modules/esbuild/lib/main.js:1258:14

Edit: this might be related: vitejs/vite#6493

@lhr0909
Copy link
Contributor

lhr0909 commented Feb 13, 2022

Ahh, thanks for submitting the issue. Currently it is built with NodeJS only packages and I need to swap out implementation for the TextEncoder and TextDecoder in order for browser usage. I will spend some time this week to do it.

I think there is an npm package called lib0 that can resolve the issue. If you have some extra time please give a hand and take a crack at it! Cheers!

@lhr0909 lhr0909 mentioned this issue Feb 15, 2022
@lhr0909
Copy link
Contributor

lhr0909 commented Feb 15, 2022

@Tymek Please try the latest v1.1.2, it should work on the browser now. Please let me know if there are any more issues!

@Niputi
Copy link

Niputi commented Feb 19, 2022

Hello @lhr0909 I have tested your update https://github.com/Niputi/vite-tests/tree/npm-gpt3-tokenizer

  • the browser dist version should end with .mjs
  • the readme is wrong. it should be import GPT3Tokenizer from 'gpt3-tokenizer' as you use default export.
  • it's possible to instantiate TextEncoder and TextDecoder in node environment without importing from util, so why import from util?

fyi. if you want to export more stuff later, it's better export code as named exports instead of defaults as default and named exports dont go well together

@bramses
Copy link

bramses commented Jun 24, 2022

This is what worked for me (node v18.1.0):

tokenizer.mjs

import GPT3Tokenizer from 'gpt3-tokenizer'

const tokenizer = new GPT3Tokenizer.default({ type: 'gpt3' })

const str = "hello 👋 world 🌍";
const encoded = tokenizer.encode(str);
const decoded = tokenizer.decode(encoded.bpe);

console.log(encoded);
console.log(decoded);

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

Successfully merging a pull request may close this issue.

4 participants