-
Hi, is there any way this library support common js , i have an existing project which is typescript so when is try to use this its throwing error like dynamic import not supported etc, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey! You can try it like this (ex. for transformers.js): import {
type PreTrainedModel,
type PreTrainedTokenizer,
} from '@xenova/transformers';
const TransformersApi = Function('return import("@xenova/transformers")')();
const CrossEncoderModelProvider: Provider = {
provide: 'CrossEncoderModel',
useFactory: async (): Promise<PreTrainedModel> => {
const { AutoModelForSequenceClassification } = await TransformersApi; // Here you can get methods from the lib.
return await AutoModelForSequenceClassification.from_pretrained(
'mixedbread-ai/mxbai-rerank-xsmall-v1',
);
},
}; |
Beta Was this translation helpful? Give feedback.
-
I don't plan to add support for CommonJS, as it'll make it much more complicated to maintain and develop, and ESM is the official way forward in nodejs. If you'd like to import this library from inside of a CommonJS project without changing the project to be an ES module, you can do this: async function myLogic() {
const {getLlama} = await import("node-llama-cpp");
const llama = await getLlama();
}
myLogic(); And if your async function myLogic() {
const nlc: typeof import("node-llama-cpp") = await Function('return import("node-llama-cpp")')();
const {getLlama} = nlc;
const llama = await getLlama();
}
myLogic(); |
Beta Was this translation helpful? Give feedback.
I don't plan to add support for CommonJS, as it'll make it much more complicated to maintain and develop, and ESM is the official way forward in nodejs.
I've explained it more in depth here.
If you'd like to import this library from inside of a CommonJS project without changing the project to be an ES module, you can do this:
And if your
tsconfig.json
is configured to transpileimport
intorequire
automatically, you can do this instead: