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

ReferenceError: importScripts is not defined when using await import #478

Closed
linonetwo opened this issue Jul 17, 2023 · 2 comments
Closed

Comments

@linonetwo
Copy link

I always get #461 for a worker, I think is is due to fail to import some deps, so I use dynamic import instead for a try

function runLLama(
  options: { conversationID: string; modelPath: string; prompt: string },
): Observable<ILanguageModelWorkerResponse> {
        const { LLM } = await import('llama-node');
        const { LLamaCpp } = await import('llama-node/dist/llm/llama-cpp');
        const llama = new LLM(LLamaCpp);
//...

const llmWorker = { runLLama };
export type LLMWorker = typeof llmWorker;
expose(llmWorker);

And I get importScripts is not defined

related compiled code

void 0!==__webpack_require__&&(__webpack_require__.ab=__dirname+"/native_modules/"),(()=>{var e={948:1};__webpack_require__.f.i=(r,t)=>{e[r]||importScripts(__webpack_require__.p+__webpack_require__.u(r))};var r=("undefined"!=typeof self?self:this).webpackChunktidgi=("undefined"!=typeof self?self:this).webpackChunktidgi||[],t=r.push.bind(r);r.push=r=>{var[n,o,i]=r;
@linonetwo
Copy link
Author

linonetwo commented Jul 17, 2023

Get this after externalize it by

  new ExternalsPlugin({
    type: 'commonjs',
    // use regex works.
    include: /@tiddlygit\+tiddlywiki@(.+)|llama-node(.+)|@llama-node(.+)/,
    // when using npm, we can use this. But with pnpm, this won't work ↓
    // include: path.join(__dirname, 'node_modules', '.pnpm', '@tiddlygit', 'tiddlywiki'),
  }),
  new ThreadsPlugin({
    target: 'electron-node-worker',
    plugins: ['ExternalsPlugin'],
  }),
require() of ES Module xx/Resources/node_modules/llama-node/dist/llm/llama-cpp.js from xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js not supported.

Instead change the require of llama-cpp.js in xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module xx/Resources/node_modules/llama-node/dist/llm/llama-cpp.js from xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js not supported.

Instead change the require of llama-cpp.js in xx/Resources/app.asar.unpacked/.webpack/main/948.index.worker.js to a dynamic import() which is available in all CommonJS modules.

@linonetwo
Copy link
Author

This works

        const { LLM } = await import('llama-node');
        const { LLamaCpp } = await import('llama-node/dist/llm/llama-cpp.cjs');

with

declare module 'llama-node/dist/llm/llama-cpp.cjs' {
  export { LLamaCpp } from 'llama-node/dist/llm/llama-cpp';
}

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

1 participant