我正在尝试将Web3添加到React项目中。 我已经使用以下命令初始化了新项目:
gatsby new
然后,我安装了web3
npm install --save web3
当我在index.js文件中引入web3时
import Web3 from 'web3'
如果我打电话
gatsby develop
我遇到了一些奇怪的错误:
ERROR in ./node_modules/eth-lib/lib/bytes.js 9:193-227 模块未找到:在“/home/test/gatsby/test/test/node_modules/eth-lib/lib”中无法解析密码。
BREAKING CHANGE:webpack < 5默认情况下会包含 node.js 核心模块的polyfill。现在不再是这种情况。请验证是否需要此模块并为其配置一个polyfill。
如果您想要包含一个polyfill,您需要: - 添加后备选项'resolve.fallback: { "assert": require.resolve("assert/") }' - 安装'assert' 如果您不想包含一个polyfill,您可以使用一个空模块,像这样: resolve.fallback: { "assert": false } @ ./node_modules/ethereumjs-util/dist/index.js 29:13-33 @ ./node_modules/ethereumjs-tx/dist/transaction.js 14:24-50 @ ./node_modules/ethereumjs-tx/dist/index.js 3:20-44 @ ./node_modules/web3-eth-accounts/lib/index.js 35:18-54 @ ./node_modules/web3-eth/lib/index.js 34:15-43 @ ./node_modules/web3/lib/index.js 30:10-29 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70如果您想包含一个 polyfill ,您需要: - 添加备用 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装'crypto-browserify' 如果您不想包含polyfill,您可以使用一个空模块,如下所示: resolve.fallback: { "crypto": false } @ ./node_modules/swarm-js/lib/api-browser.js 32:12-40 @ ./node_modules/web3-bzz/lib/index.js 24:12-31 @ ./node_modules/web3/lib/index.js 34:10-29 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
ERROR in ./node_modules/ethereumjs-util/dist/account.js 4:13-30 模块未找到:在“/home/test/gatsby/test/test/node_modules/ethereumjs-util/dist”中无法解析“assert”。
BREAKING CHANGE:webpack < 5默认情况下会包含 node.js 核心模块的polyfill。现在不再是这种情况。请验证是否需要此模块并为其配置一个polyfill。
错误在./node_modules/ethereumjs-util/dist/object.js 4:13-30中发生: 找不到模块:错误:无法解析'assert'在'/home/test/gatsby/test/test/node_modules/ethereumjs-util/dist'中。
重大变更:webpack <5默认情况下会包括node.js核心模块的polyfills。 现在不再是这种情况。请确认是否需要此模块并为其配置一个polyfill。
如果您想要包含一个polyfill,您需要: - 添加后备选项'resolve.fallback: { "assert": require.resolve("assert/") }' - 安装'assert' 如果您不想包含一个polyfill,您可以使用一个空模块,像这样: resolve.fallback: { "assert": false } @ ./node_modules/ethereumjs-util/dist/index.js 45:13-32 @ ./node_modules/ethereumjs-tx/dist/transaction.js 14:24-50 @ ./node_modules/ethereumjs-tx/dist/index.js 3:20-44 @ ./node_modules/web3-eth-accounts/lib/index.js 35:18-54 @ ./node_modules/web3-eth/lib/index.js 34:15-43 @ ./node_modules/web3/lib/index.js 30:10-29 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/web3-eth-accounts/lib/index.js 30:76-93处出现错误: 模块未找到:在'/home/test/gatsby/test/test/node_modules/web3->eth-accounts/lib'中无法解析'crypto'
重大变更:webpack < 5默认情况下会包含node.js核心模块的polyfill,但现在不再如此。请验证是否需要此模块并为其配置polyfill。
如果您想要包含polyfill,则需要: - 添加回退 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装 'crypto-browserify' 如果您不想包含polyfill,则可以使用空模块,如下所示: resolve.fallback: { "crypto": false } @ ./node_modules/web3-eth/lib/index.js 34:15-43 @ ./node_modules/web3/lib/index.js 30:10-29 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/web3-eth-accounts/node_modules/eth-lib/lib/bytes.js 7:193-227处出现错误: 模块未找到:在'/home/test/gatsby/test/test/node_modules/web3-eth-accounts/node_modules/eth-lib/lib'中无法解析'crypto'
重大变更:webpack < 5默认情况下会包含node.js核心模块的polyfill,但现在不再如此。请验证是否需要此模块并为其配置polyfill。
如果您想要包含polyfill,则需要: - 添加回退 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }' - 安装 'crypto-browserify' 如果您不想包含polyfill,则可以使用空模块,如下所示: resolve.fallback: { "crypto": false } @ ./node_modules/web3-eth-accounts/lib/index.js 29:12-40 @ ./node_modules/web3-eth/lib/index.js 34:15-43 @ ./node_modules/web3/lib/index.js 30:10-29 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/web3-providers-http/lib/index.js 26:11-26出现错误 模块未找到:错误:无法在'/home/test/gatsby/test/test/node_modules/web3-providers-http/lib'中解析'http'
BREAKING CHANGE: webpack < 5默认情况下会包含node.js核心模块的polyfill。 现在不再是这种情况。验证您是否需要此模块并为其配置polyfill。
如果要包含polyfill,您需要: - 添加回退'resolve.fallback: { "http": require.resolve("stream-http") }' - 安装'stream-http' 如果不想包含polyfill,可以像这样使用一个空模块: resolve.fallback: { "http": false } @ ./node_modules/web3-core-requestmanager/lib/index.js 46:18-48 @ ./node_modules/web3-core/lib/index.js 22:23-58 @ ./node_modules/web3/lib/index.js 29:11-31 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/web3-providers-http/lib/index.js 27:12-28出现错误 模块未找到:错误:无法在'/home/test/gatsby/test/test/node_modules/web3-providers-http/lib'中解析'https'
BREAKING CHANGE: webpack < 5默认情况下会包含node.js核心模块的polyfill。 现在不再是这种情况。验证您是否需要此模块并为其配置polyfill。
如果要包含polyfill,您需要: - 添加回退'resolve.fallback: { "https": require.resolve("https-browserify") }' - 安装'https-browserify' 如果不想包含polyfill,可以像这样使用一个空模块: resolve.fallback: { "https": false } @ ./node_modules/web3-core-requestmanager/lib/index.js 46:18-48 @ ./node_modules/web3-core/lib/index.js 22:23-58 @ ./node_modules/web3/lib/index.js 29:11-31 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/xhr2-cookies/dist/xml-http-request.js 21:11-26处出现错误 模块未找到:错误:无法解析“/home/test/gatsby/test/test/node_modules/xhr2-cookies/dist”中的“http”
BREAKING CHANGE: webpack < 5默认情况下会包含node.js核心模块的polyfills。 这不再是默认情况。请验证是否需要此模块并为其配置polyfill。
如果您想包含polyfill,则需要: - 添加后备“resolve.fallback:{“http”:require.resolve(“stream-http”)}” - 安装'stream-http' 如果您不想包含polyfill,则可以使用空模块,如下所示: resolve.fallback:{“http”:false} @ ./node_modules/xhr2-cookies/dist/index.js 6:9-38 @ ./node_modules/web3-providers-http/lib/index.js 25:11-49 @ ./node_modules/web3-core-requestmanager/lib/index.js 46:18-48 @ ./node_modules/web3-core/lib/index.js 22:23-58 @ ./node_modules/web3/lib/index.js 29:11-31 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
在./node_modules/xhr2-cookies/dist/xml-http-request.js 22:12-28处出现错误 模块未找到:错误:无法解析“/home/test/gatsby/test/test/node_modules/xhr2-cookies/dist”中的“https”
如果你想要包含一个 polyfill,你需要: - 添加回退 'resolve.fallback: { "https": require.resolve("https-browserify") }' - 安装 'https-browserify' 如果你不想包含一个 polyfill,你可以使用一个空模块,像这样: resolve.fallback: { "https": false } @ ./node_modules/xhr2-cookies/dist/index.js 6:9-38 @ ./node_modules/web3-providers-http/lib/index.js 25:11-49 @ ./node_modules/web3-core-requestmanager/lib/index.js 46:18-48 @ ./node_modules/web3-core/lib/index.js 22:23-58 @ ./node_modules/web3/lib/index.js 29:11-31 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70BREAKING CHANGE: webpack < 5默认情况下会包含node.js核心模块的polyfills。 这不再是默认情况。请验证是否需要此模块并为其配置polyfill。
ERROR in ./node_modules/xhr2-cookies/dist/xml-http-request.js 23:9-22 模块未找到:错误:无法在 '/home/test/gatsby/test/test/node_modules/xhr2-cookies/dist' 中解析 'os'
重大变化:webpack < 5 默认情况下会为 node.js 核心模块包含 polyfill。 现在不再是这种情况。请验证您是否需要此模块,并为其配置 polyfill。
如果你想要包含一个 polyfill,你需要: - 添加回退 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }' - 安装 'os-browserify' 如果你不想包含一个 polyfill,你可以使用一个空模块,像这样: resolve.fallback: { "os": false } @ ./node_modules/xhr2-cookies/dist/index.js 6:9-38 @ ./node_modules/web3-providers-http/lib/index.js 25:11-49 @ ./node_modules/web3-core-requestmanager/lib/index.js 46:18-48 @ ./node_modules/web3-core/lib/index.js 22:23-58 @ ./node_modules/web3/lib/index.js 29:11-31 @ ./src/pages/index.js 9:0-24 @ ./.cache/this_is_virtual_fs_path/$virtual/async-requires.js 21:11-23:5 @ ./.cache/app.js 17:0-52 28:0-70 30:27-40 28:0-70
webpack 编译出现了 10 个错误
我已尝试手动安装这些模块、将它们添加到 package.json 等等。但是什么都不起作用。
你能帮我吗?