首页 > 解决方案 > 如何使用 ethereumjs-tx | 没有错误:Tx 不是构造函数?

问题描述

我正在尝试包含 etheruemjs-tx 浏览器构建,以便在我的浏览器上使用它来签署交易。它不断抛出错误“Tx 不是构造函数”

const Tx = "https://github.com/ethereumjs/browser-builds/blob/master/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.js";

在我的终端上一切正常,因为我刚刚使用

const Tx = require('ethereumjs-tx').Transaction

但由于我不能在我的 javascript 代码上使用 require(),所以我一直在尝试各种方法,但我无法让它工作。单击按钮后,在我的浏览器控制台上,它转到此行..

const tx = new Tx(txObject,{chain: 'ropsten', hardfork: 'petersburg'})

错误是:(index):368 Uncaught TypeError: Tx is not a constructor

非常感谢有关如何在浏览器中使用 ethereumjs-tx 的帮助。

标签: javascriptblockchainethereumweb3

解决方案


在新版本中,您应该更改:

const Tx = require('ethereumjs-tx')

const Tx = require('ethereumjs-tx').Transaction

推荐阅读