[英]Error Library web3 and Webpack in react and angular
我使用“react”:“^17.0.2”,我试图通过 npm i web3 添加 web3 库但是当我尝试像这样实例化我的 web3 对象时 web3 = new Web3((window as any).ethereum); 或者像这样 web3 = new Web3();
然后我尝试启动我的应用程序 (npm start)
我收到很多错误
Compiled with problems:
错误在./node_modules/cipher-base/index.js 3:16-43
找不到模块:错误:无法解析“C:...\node_modules\cipher-base”中的“流”
重大变化:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 这已不再是这种情况。 验证您是否需要此模块并为其配置一个 polyfill。
如果你想包含一个 polyfill,你需要:
- 添加后备 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
- 安装 'stream-browserify' 如果你不想包含 polyfill,你可以使用这样的空模块:resolve.fallback: { "stream": false }
错误在./node_modules/eth-lib/lib/bytes.js 9:193-227
找不到模块:错误:无法解析“crypto”...
错误在./node_modules/ethereumjs-util/dist.browser/account.js 71:31-48
找不到模块:错误:无法在...中解析“断言”
错误在./node_modules/ethereumjs-util/dist.browser/address.js 14:31-48
找不到模块:错误:无法解析“断言”...错误在./node_modules/ethereumjs-util/dist.browser/object.js 46:31-48
找不到模块:错误:无法在...中解析“断言”
错误在./node_modules/web3-eth-accounts/lib/index.js 31:74-91
找不到模块:错误:无法解析“加密”...错误在./node_modules/web3-eth-accounts/node_modules/eth-lib/lib/bytes.js 7:193-227
找不到模块:错误:无法在...中解析“加密”
错误在./node_modules/web3-providers-http/lib/index.js 30:11-26
找不到模块:错误:无法在...中解析“http”
错误在./node_modules/web3-providers-http/lib/index.js 32:12-28
找不到模块:错误:无法解析“https”...
错误在./node_modules/xhr2-cookies/dist/xml-http-request.js 37:11-26
找不到模块:错误:无法在...中解析“http”
错误在./node_modules/xhr2-cookies/dist/xml-http-request.js 39:12-28
找不到模块:错误:无法解析“https”...
错误在./node_modules/xhr2-cookies/dist/xml-http-request.js 41:9-22
找不到模块:错误:无法解析 'C:...\node_modules\xhr2-cookies\dist' 中的 'os'
重大变化:webpack < 5 用于默认包含 node.js 核心模块的 polyfill。 这已不再是这种情况。 验证您是否需要此模块并为其配置一个 polyfill。
如果你想包含一个 polyfill,你需要:
- 添加后备 'resolve.fallback: { "os": require.resolve("os-browserify/browser") }'
- install 'os-browserify' 如果你不想包含 polyfill,你可以像这样使用一个空模块:resolve.fallback: { "os": false }
警告 in./node_modules/@ethersproject/abi/lib.esm/coders/fixed-bytes.js 模块警告(来自./node_modules/source-map-loader/dist/cjs.js):无法从“解析源映射” C:...\node_modules@ethersproject\abi\src.ts\coders\fixed-bytes.ts' 文件:错误:ENOENT:没有这样的文件或目录,打开 'C:...\node_modules@ethersproject\abi\ src.ts\coders\fixed-bytes.ts' @./node_modules/@ethersproject/abi/lib.esm/abi-coder.js 13:0-55 76:17-32 @./node_modules/@ethersproject/abi /lib.esm/index.js 4:0-56 6:0-204 6:0-204 @./node_modules/web3-eth-abi/lib/index.js 28:21-59 30:16-55 @ ./node_modules/web3-eth/lib/index.js 47:10-33 @./node_modules/web3/lib/index.js 34:10-29 @./src/components/Authenticate.tsx 7:0-24 14:19-23 14:24-42 @./src/App.tsx 5:0-53 10:35-47 @./src/index.tsx 7:0-24 11:33-36
警告 in./node_modules/@ethersproject/abi/lib.esm/coders/null.js 模块警告(来自./node_modules/source-map-loader/dist/cjs.js):无法从 'C: 解析源映射...\node_modules@ethersproject\abi\src.ts\coders\null.ts' 文件:错误:ENOENT:没有这样的文件或目录,打开 'C:...\node_modules@ethersproject\abi\src.ts\编码器\null.ts' @./node_modules/@ethersproject/abi/lib.esm/abi-coder.js 14:0-42 50:19-28 @./node_modules/@ethersproject/abi/lib.esm/index .js 4:0-56 6:0-204 6:0-204 @./node_modules/web3-eth-abi/lib/index.js 28:21-59 30:16-55 @./node_modules/web3- eth/lib/index.js 47:10-33 @./node_modules/web3/lib/index.js 34:10-29 @./src/components/Authenticate.tsx 7:0-24 14:19-23 14 :24-42 @./src/App.tsx 5:0-53 10:35-47 @./src/index.tsx 7:0-24 11:33-36
我使用了 npx create-react-app Appname --template typescript 给出的基本模板,然后添加了 web3 库 (npm i web3)
并尝试了这段代码
从“反应”中导入反应,{useState}; 从 '@metamask/detect-provider' 导入 detectEthereumProvider 从 'web3' 导入 Web3;
const 验证 = () => {
const web3 = new Web3();
}
return (
<div>
TEST
</div >
);
};
导出默认验证;
安装反应脚本@4.0.3
npm install --save react-scripts@4.0.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.