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

[Vue/Vite/Nuxt] Build Error: require is not defined in ES module scope, you can use import instead #353

Open
k2m5t2 opened this issue Apr 1, 2024 · 4 comments

Comments

@k2m5t2
Copy link

k2m5t2 commented Apr 1, 2024

Environment

Problem

Using web-llm in a Nuxt project results in an import error during build.

This seems to relate to ES module / CommonJS import conflicts.

 ERROR  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 '/Users/yunhocho/Documents/GitHub/tvm-bot-web/frontend/node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/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.

  This file is being treated as an ES module because it has a '.js' file extension and 'node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/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 <anonymous> (node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/node_modules/@mlc-ai/web-llm/node_modules/@mlc-ai/web-tokenizers/lib/index.js:66:73)
  at <anonymous> (node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/node_modules/@mlc-ai/web-llm/node_modules/@mlc-ai/web-tokenizers/lib/index.js:78:4)
  at <anonymous> (node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/node_modules/@mlc-ai/web-llm/node_modules/@mlc-ai/web-tokenizers/lib/index.js:2:66)
  at <anonymous> (node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/node_modules/@mlc-ai/web-llm/node_modules/@mlc-ai/web-tokenizers/lib/index.js:5:2)
  at <anonymous> (node_modules/.pnpm/@mlc-ai+web-llm@0.2.29/node_modules/@mlc-ai/web-llm/node_modules/@mlc-ai/web-tokenizers/lib/index.js:204:1)
  at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
  at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
  at async ViteNodeRunner.interopedImport (node_modules/.pnpm/vite-node@1.4.0/node_modules/vite-node/dist/client.mjs:383:28)
  at async ViteNodeRunner.directRequest (node_modules/.pnpm/vite-node@1.4.0/node_modules/vite-node/dist/client.mjs:253:24)
  at async ViteNodeRunner.cachedRequest (node_modules/.pnpm/vite-node@1.4.0/node_modules/vite-node/dist/client.mjs:189:14)

(Temporary) Solution

In nuxt.config.ts, add the build transpile option:

export default defineNuxtConfig({
+   build: {
+     transpile: ['@mlc-ai/web-llm'], // required in order to build successfully
+  },
...

and change import statements like the following:

- import { ChatInterface, ChatModule, ChatRestModule, ChatWorkerClient, AppConfig } from "@mlc-ai/web-llm";
+ import * as webllm from '@mlc-ai/web-llm'

- chat.value = new ChatModule(); // example usage
+ const chat = new webllm.ChatModule() // example usage

However, this does seem to be an abnormal behavior that still needs to be resolved.

Related Issues

#140

Notes

I don't have expertise in JavaScript/Vue/Nuxt to fully understand what is going on, so please add any other helpful notes!

Feel free to close this, or keep until the error doesn't occur by default

@wingstako
Copy link

Same error on my SvelteKit + Vite project

@kikohs
Copy link

kikohs commented May 3, 2024

Having a langchain integration would be fantastic for web-llm. Bumping the issue.

@wingstako
Copy link

Same error on my SvelteKit + Vite project

It works after disabling SSR

@CharlieFRuan
Copy link
Contributor

CharlieFRuan commented May 21, 2024

This error should be addressed in npm 0.2.36. For details please see #397.

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

4 participants