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

Next example doesn't work unless source is built #140

Closed
narangkay opened this issue Jul 4, 2023 · 11 comments
Closed

Next example doesn't work unless source is built #140

narangkay opened this issue Jul 4, 2023 · 11 comments

Comments

@narangkay
Copy link
Contributor

The next example only seems to work when the source is built locally, and not when it's installed from npm.

Weirdly enough, if I install the source locally and then switch to using the prebuilt npm package, it works :/

Trial 1: Using prebuilt npm package only

  1. Don't build the web-llm repo from source
  2. Set the dependency in package,json to the prebuilt package
...
    "@mlc-ai/web-llm": "^0.2.2",
...
  1. install and run
npm install
npm run dev
  1. Run fails

Trial 2: Using prebuilt npm package after locally installing web-llm

  1. Build the web-llm repo from source
  2. Set the dependency in package,json to the prebuilt package
...
    "@mlc-ai/web-llm": "^0.2.2",
...
  1. install and run
npm install
npm run dev
  1. Run succeeds

Trial 3: Building from source

  1. Build the web-llm repo from source
  2. Set the dependency in package,json to the local package
...
    "@mlc-ai/web-llm": "../..",
...
  1. install and run
npm install
npm run dev
  1. Run succeeds

I'm a bit flummoxed as to why 1 doesn't work but 2 and 3 work. Specially 2 is surprising to me.

@tqchen
Copy link
Contributor

tqchen commented Jul 4, 2023

Get it.. this is surprising, would be useful to look into the difference. I wonder if it has to do with way things are bundled

tqchen pushed a commit that referenced this issue Jul 7, 2023
As described in #140, for some
reason the prebuilt package isn't working.
@djaffer
Copy link

djaffer commented Jul 9, 2023

getting this error:

This file is being treated as an ES module because it has a '.js' file extension and '\web-llm\examples\next-simple-chat\node_modules\@mlc-ai\web-llm\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3612:76
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3624:7
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3556:5
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3557:3
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3751:2
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  digest: undefined
}
- error Error [ReferenceError]: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '\web-llm\examples\next-simple-chat\node_modules\@mlc-ai\web-llm\package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3612:76
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3624:7
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3556:5
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3557:3
    at file:///web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/lib/index.js:3751:2
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {

@narangkay
Copy link
Contributor Author

Just to confirm, you're seeing that error when using the pre built binary from npm, and not when building from source, correct? if yes, it matches my error as well

@djaffer
Copy link

djaffer commented Jul 9, 2023

yes. Right. Building a source is not possible. If it is required, why not just upload the binaries in a seperate repo?

@narangkay
Copy link
Contributor Author

To clarify a bit
Before #138 it was not possible to use this package with NextJS (by building from source or by using the package from npm).
After #138 it's now possible to build the project from source and then use it with NextJS, but for some reason the pre-built binary with npm is not working.

See instructions to build form source here: https://github.com/mlc-ai/web-llm#build-webllm-package-from-source

@narangkay
Copy link
Contributor Author

Would be helpful if you could provide more information about why building from source did not work, and if the issue was with the next integration specifically or you were not able to build from source at all.

I have a working example here which works with next js, complete with a script to build from source on vercel.

https://github.com/narangkay/interactive-resume

@narangkay
Copy link
Contributor Author

Also, I think if your problems are unrelated to the next integration and more about inability to build from source, you might want to create a new issue

@praveen-palanisamy
Copy link

With any Next.js project, the examples/next-simple-chat project, when the pre-built Web LLM package from the NPM package registry @mlc-ai/web-llm (as of v0.2.8) is installed and used directly, the following ReferenceError is raised:

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '.../web-llm/examples/next-simple-chat/node_modules/@mlc-ai/web-llm/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

One way to fix the above ES Module and CommonJS syntax mixing is by leveraging Next.js's support for automatically transpiling dependencies from packages.

With Next.js v13.0.0+, adding the following to next.config.js is all that is needed:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ['@mlc-ai/web-llm'],
  ...
}
 
module.exports = nextConfig

So, we could update examples/next-simple-chat/next.config.js with the above line to get rid of the above ReferenceError.

But that results in the following compiler error:

- error ./node_modules/@mlc-ai/web-llm/lib/index.js
Module parse failed: Identifier 'Module' has already been declared (6895:20)
|             return async function() {
|                 let Module = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
>                 var Module = typeof Module != "undefined" ? Module : {};
|                 var readyPromiseResolve, readyPromiseReject;
|                 Module["ready"] = new Promise(function(resolve, reject) {

Import trace for requested module:
./node_modules/@mlc-ai/web-llm/lib/index.js
./src/utils/chat_component.tsx

Her's the diff of the pre-built mlc-ai/web-llm/lib/index.js (right-side) pushed to the NPM package registry and a fresh source-build (left-side):
image

The above error is likely because of the redefinition of Module in function EmccWASI().

The rollup.config.js and examples/next-simple-chat/next.config.js doesn't seem to be in sync to produce the same bundle/compiled output.

If the use of CommonJS cannot be avoided in the bundle, a solution to consider is to conditionally export CommonJS for require and ESM for import usages in other projects as dependencies.

@narangkay
Copy link
Contributor Author

Hi @praveen-palanisamy, thank you so much for helping here - I did not have the NextJS expertise necessary to understand what was wrong.

I was able to follow along in your explanation until the following line:

The [rollup.config.js](https://github.com/mlc-ai/web-llm/tree/main/rollup.config.js) and [examples/next-simple-chat/next.config.js](https://github.com/mlc-ai/web-llm/tree/main/examples/next-simple-chat/next.config.js) doesn't seem to be in sync to produce the same bundle/compiled output.

I would love to fix this, since it seems like it would fix the root of the problem. Do you have any ideas on how we can sync the two?

@Forpee
Copy link

Forpee commented Dec 16, 2023

Is there any progress on this issue?

@CharlieFRuan
Copy link
Contributor

CharlieFRuan commented May 21, 2024

This error should be addressed in npm 0.2.36 -- the next-simple-chat example should work out of the box.
For details see #397.

@tqchen tqchen closed this as completed May 31, 2024
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

6 participants