I am running a vite.js
app with web3 installed.
When I run the app in dev mode, all works fine but when I run it in production mode (build) it fails with:
"TypeError: Cannot read properties of undefined (reading 'call')"
.
I can confirm that the error comes from the contract method generated from my ABI:
contract.methods.isOwner(sender).call
({from: sender}, function (err, res)
If I comment this line out I wont get the error.
You can reproduce the error by using my test repo: download my test repo: https://github.com/nybroe/web3_vite_call_of_undefined/tree/main
follow the readme with repo steps:
setup:
- download the repro
- navigate to "app"
- npm install
dev test (which works)
- npm run dev
- check the console - no errors
build test (which breaks)
- npm run build
- npm run preview
- check the console - you will see the following errors: "TypeError: Cannot read properties of undefined (reading 'call')"
CodePudding user response:
https://stackoverflow.com/a/69021714
I use the option 2
In your vite.config.js
, add web3
:
import { defineConfig } from 'vite'
export default defineConfig({
⋮
resolve: {
alias: {
web3: 'web3/dist/web3.min.js',
},
// or
alias: [
{
find: 'web3',
replacement: 'web3/dist/web3.min.js',
},
],
},
})