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: require is not defined' occurring in production build only #4593

Closed
7 tasks done
thekevinbrown opened this issue Aug 13, 2021 · 3 comments
Closed
7 tasks done

Comments

@thekevinbrown
Copy link

Describe the bug

I'm trying to use the web3-react library with Vite. It requires some shims, which I've successfully added. When running in development mode, the library works fine, while in a production build, we get:

ReferenceError: require is not defined
    at index.6c14cda2.js:11138

Reproduction

https://github.com/CoinageCrypto/vite-web3-react-reproduction

System Info

System:
    OS: macOS 11.5.1
    CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
    Memory: 197.22 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.20.0 - ~/.nvm/versions/node/v12.20.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.20.0/bin/npm
    Watchman: 2021.06.07.00 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 92.1.27.109
    Chrome: 92.0.4515.131
    Firefox: 72.0.2
    Safari: 14.1.2
  npmPackages:
    vite: ^2.4.4 => 2.4.4

Used Package Manager

yarn

Logs

Vite build is successful. Log:


$ yarn build --debug
yarn run v1.22.10
$ tsc && vite build --debug
  vite:config bundled config file loaded in 84ms +0ms
  vite:config using resolved config: {
  vite:config   plugins: [
  vite:config     'alias',
  vite:config     'react-refresh',
  vite:config     'vite:dynamic-import-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'rollup-plugin-dynamic-import-variables',
  vite:config     'asset-import-meta-url',
  vite:config     'vite:import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:reporter',
  vite:config     'load-fallback'
  vite:config   ],
  vite:config   build: {
  vite:config     target: [ 'es2019', 'edge88', 'firefox78', 'chrome87', 'safari13.1' ],
  vite:config     polyfillDynamicImport: false,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     minify: false,
  vite:config     terserOptions: {},
  vite:config     cleanCssOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     brotliSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null
  vite:config   },
  vite:config   resolve: { dedupe: undefined, alias: [ [Object], [Object], [Object] ] },
  vite:config   configFile: '/Users/kevin/development/vite-web3-react-reproduction/vite.config.ts',
  vite:config   configFileDependencies: [ 'vite.config.ts' ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/kevin/development/vite-web3-react-reproduction',
  vite:config   base: '/',
  vite:config   publicDir: '/Users/kevin/development/vite-web3-react-reproduction/public',
  vite:config   cacheDir: '/Users/kevin/development/vite-web3-react-reproduction/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   isProduction: true,
  vite:config   server: { fs: { strict: undefined, allow: [Array] } },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen]
  vite:config   },
  vite:config   createResolver: [Function: createResolver],
  vite:config   optimizeDeps: { esbuildOptions: { keepNames: undefined } }
  vite:config } +4ms
vite v2.4.4 building for production...
✓ 363 modules transformed.
dist/assets/favicon.17e50649.svg   1.49kb
dist/index.html                    0.52kb
dist/assets/index.8c527f3b.css     0.40kb / brotli: 0.23kb
dist/assets/index.49b07601.js      1.93kb / brotli: 0.81kb
dist/assets/index.6c14cda2.js      365.85kb / brotli: 66.72kb
dist/assets/vendor.912106e6.js     656.43kb / brotli: skipped (large chunk)
✨  Done in 7.73s.

Validations

@thekevinbrown
Copy link
Author

It looks like the requires are coming from this file: https://github.com/metrowallet/walletconnect-monorepo/blob/latest/packages/providers/web3-provider/src/index.ts#L14

Which strangely has imports and requires coexisting. This may not be a Vite issue, but if there's anything I can do in Vite to work around it it'd be good to know about.

@thekevinbrown
Copy link
Author

I resolved that error, but then hit

TypeError: brorand.Rand is not a constructor
    at new MillerRabin$2 (index.85c518f5.js:22986)
    at index.85c518f5.js:23075
(anonymous) @ index.cfdc00a0.js:20

I think this is a circular dependency issue.

I think the only problems here are not Vite problems but just known behaviour of Rollup, so I'm going to close this issue.

@wangzongming
Copy link

You can do it this way

window.require = (name) => new URL(name, import.meta.url).href;

@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants