Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERR_REQUIRE_ESM for import with @xenova/transformers

In my NodeJS app I import like this:

import { AutoModel, AutoTokenizer } from '@xenova/transformers';

And then use it like so:

const tokenizer = await AutoTokenizer.from_pretrained('bert-base-uncased');

However when I run my script I get this:

> node --experimental-modules lib/index.js

No storage option exists to persist the session, which may result in unexpected behavior when using auth.
        If you want to set persistSession to true, please provide a storage option or you may set persistSession to false to disable this warning.
Starting script...
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/me/Projects/node_modules/@xenova/transformers/src/transformers.js from /home/me/Projects/lib/pinecone/utils.js not supported.
Instead change the require of transformers.js in /home/me/Projects/lib/pinecone/utils.js to a dynamic import() which is available in all CommonJS modules.
    at /home/me/Projects/lib/pinecone/utils.js:34:81
    at async syncProfilesOnPinecone (/home/me/Projects//lib/pinecone/utils.js:34:33)
    at async main (/home/me/Projects/lib/index.js:9:5) {
  code: 'ERR_REQUIRE_ESM'
}

Not sure what to do?

like image 255
strangeQuirks Avatar asked Oct 21 '25 05:10

strangeQuirks


1 Answers

Creator of Transformers.js here.

This is a problem not specific to transformers.js (for the most part), and occurs because of your node resolution settings in tsconfig.json. By default, all import statements are converted to require during compilation.

You can fix this as follows:

const TransformersApi = Function('return import("@xenova/transformers")')();
const { pipeline } = await TransformersApi;

which basically just delays the import to after compilation. See here for more information.

like image 79
Xenova Avatar answered Oct 23 '25 21:10

Xenova