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

Failed to serve on production: Uncaught ReferenceError: require is not defined #20

Closed
johnson86tw opened this issue Nov 16, 2021 · 2 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@johnson86tw
Copy link
Member

johnson86tw commented Nov 16, 2021

Because Vite is only for ES module, I used resolve.alias to solve the problem that Vite can't import the built-in node.js package from the third-party modules. Most of the effort is to resolve @walletconnect/web3-provider. There're lots of node.js built-in packages being used.

See these lines:

But in this scenario, when I build the demo files and run yarn serve:demo, it failed to run the app and give me: require is not defined.

This is hard to resolve. I don't know why yarn dev works but it failed on production.

Reference

@johnson86tw johnson86tw added the bug Something isn't working label Nov 16, 2021
@johnson86tw johnson86tw added the help wanted Extra attention is needed label Nov 16, 2021
@johnson86tw johnson86tw changed the title Failed to serve static files: Uncaught ReferenceError: require is not defined Failed to serve on production: Uncaught ReferenceError: require is not defined Nov 19, 2021
@johnson86tw
Copy link
Member Author

The solution of this issue is using build.rollupOptions.plugins and build.commonjsOptions.transformMixedEsModules in the vite.config.ts, see below:

// vite.config.ts
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import { defineConfig } from 'vite'
import WindiCSS from 'vite-plugin-windicss'
import inject from '@rollup/plugin-inject'

export default defineConfig({
  root: 'demo/',
  plugins: [vue(), WindiCSS()],
  resolve: {
    alias: {
      'vue-dapp': resolve(__dirname, './src/index.ts'),
    },
  },
  build: {
    rollupOptions: {
      plugins: [inject({ Buffer: ['buffer', 'Buffer'] })],
    },
    commonjsOptions: {
      transformMixedEsModules: true,
    },
  },
})

It comes from following two discussions:

@johnson86tw
Copy link
Member Author

New solution implemented in PR #98 and vue3-dapp-starter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant