Skip to content

Conversation

@derek-wangpch
Copy link

Description of change

There is error when using node-llama-cpp in a electron + webpack + commonjs environment, where node-llama-cpp is imported using await import('node-llama-cpp') in Typescript.

Modules that do not exist will result in errors when building with webpack

ERROR in ./node_modules/node-llama-cpp/dist/bindings/utils/compileLLamaCpp.js 270:52-85
Module not found: Error: Can't resolve '@node-llama-cpp/mac-x64'

Tested locally with

  1. electron + wepack + commonjs
  2. electron + vite + esm

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply eslint formatting
  • npm run test passes with this change
  • This pull request links relevant issues as Fixes #0000 N/A
  • There are new or updated unit tests validating the change N/A
  • Documentation has been updated to reflect this change N/A - can do later
  • The new commits and pull request title follow conventions explained in pull request guidelines (PRs that do not follow this convention will not be merged)

@giladgd
Copy link
Member

giladgd commented Oct 13, 2024

@derek-wangpch Thanks for the PR!

Bundling node-llama-cpp is not supported because its original file structure is crucial for it to function correctly.

The prebuilt binaries consist of multiple files, and their structure and naming are important (otherwise, they'll fail to load).
When bundling the code, Webpack can only recognize the entry binary file (assuming it's hard-coded), and will rename it by default.
To circumvent that, you'll need extensive Webpack configuration in your project to ensure the bundled binary files are structured properly, which is undesirable.

Also, there are many parts of the code where binaries are loaded dynamically and there's no way around it (like loading binaries that are built from source using the source download command).

And this is only one side where bundling node-llama-cpp breaks it.

The resolution of the correct prebuilt binaries module is the first obstacle you'd encounter when trying to bundle it, but it won't be the last.
Also, CommonJS is not supported on purpose due to the maintenance complexity it adds and ESM being the official way forward in nodejs. I've explained it more in depth here.

To use node-llama-cpp in Electron with Vite or Webpack, I recommend marking node-llama-cpp an an external module, and packing it via ASAR.
The scaffolded Electron project template already has that configured.

I'll update the Electron docs to include this explanation.

@giladgd giladgd closed this Oct 13, 2024
@derek-wangpch
Copy link
Author

@giladgd Thanks for your explanation!

I have looked into the electron scaffold project (nice work by the way!), it is using vite. I am using webpack because it can delete the unnecessary files and make much smaller electron app bundle. But it is quite painful to make my project work with ESM given its legacy dependencies. That's why I created this PR as a workaround to unblock.

Let's me try if marking it an external module helps.

Appreciate the comments, thanks!

@giladgd
Copy link
Member

giladgd commented Oct 14, 2024

@derek-wangpch I recommend you to try the suggested ESM workarounds to use node-llama-cpp in a transpiled CommonJS project (like Webpack).
It may help you use it without changing any Webpack configuration (except ensuring that node-llama-cpp is shipped in the .asar archive).

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 this pull request may close these issues.

2 participants