Home > Back-end >  TypeError: Cannot read properties of undefined (reading 'call') on build but not dev
TypeError: Cannot read properties of undefined (reading 'call') on build but not dev

Time:04-29

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:

  1. download the repro
  2. navigate to "app"
  3. npm install

dev test (which works)

  1. npm run dev
  2. check the console - no errors

build test (which breaks)

  1. npm run build
  2. npm run preview
  3. 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',
      },
    ],
  },
})
  • Related