我正在運行一個vite.js安裝了 web3 的應用程式。當我在開發模式下運行應用程式時,一切正常,但是當我在生產模式(構建)下運行它時,它會失敗并顯示:
"TypeError: Cannot read properties of undefined (reading 'call')".
我可以確認錯誤來自我的 ABI 生成的合約方法:contract.methods.isOwner(sender) .call({from: sender}, function (err, res)
如果我將此行注釋掉,我將不會收到錯誤訊息。
您可以使用我的測驗存盤庫重現錯誤:下載我的測驗存盤庫: https ://github.com/nybroe/web3_vite_call_of_undefined/tree/main
按照 repo 步驟的自述檔案:
設定:
- 下載復制品
- 導航到“應用程式”
- npm 安裝
開發測驗(有效)
- npm 運行開發
- 檢查控制臺 - 沒有錯誤
構建測驗(中斷)
- npm 運行構建
- npm 運行預覽
- 檢查控制臺 - 您將看到以下錯誤:“TypeError: Cannot read properties of undefined (reading 'call')”
uj5u.com熱心網友回復:
https://stackoverflow.com/a/69021714
我使用選項 2
在你的vite.config.js,添加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',
},
],
},
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/467762.html
