找不到模块:错误:无法解析“web3”

问题描述 投票:0回答:1

我正在尝试将 web3 模块导入到我的代码中,但代码似乎无法识别。我尝试过使用 npm install web3,但它使整个事情失败了。请问这个问题有什么解决办法吗?我也尝试删除 node_modules 并使用 npm install 重新安装,但它仍然不起作用。

下面是我的代码:

//usePools.js
import Web3 from  'web3';
import { useState, useEffect } from 'react';
import { useConfig } from '@usedapp/core';

import { ROUTER_ADDRESS } from '../config';
import { getFactoryInfo, getRouterInfo } from '../utils';

export const loadPools = async (providerUrl) => {
    const provider = new Web3.providers.HttpProvider(providerUrl);
    const web3 = new Web3(provider);

    const routerInfo =  await getRouterInfo(ROUTER_ADDRESS, web3);
    const factoryInfo = await getFactoryInfo(routerInfo.factory, web3);

    return factoryInfo.pairsInfo;

}


export const usePools = () => {
    const { readOnlyChainId, readOnlyUrls } = useConfig();
    const [loading, setLoading] = useState(true);
    const [pools, setPools] = useState({});


    useEffect(() =>{
        loadPools(readOnlyUrls[readOnlyChainId])
        .then((pools) => {
            setPools(pools)
            setLoading(false)
        })
    }, [readOnlyUrls, readOnlyChainId]);
    

    return [loading, pools];
}
//Package.json
{
  "name": "@my-app/react-app",
  "version": "1.0.0",
  "homepage": "./",
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "dependencies": {
    "@my-app/contracts": "^1.0.0",
    "@apollo/client": "^3.5.10",
    "@ethersproject/contracts": "^5.6.0",
    "@ethersproject/providers": "^5.6.0",
    "@testing-library/dom": "^8.11.3",
    "@testing-library/jest-dom": "^5.16.2",
    "@testing-library/react": "^12.1.4",
    "@testing-library/user-event": "^13.5.0",
    "@types/react": "^17.0.40",
    "graphql": "^16.3.0",
    "ipfs-deploy": "^11.2.0",
    "react": "17.0.2",
    "react-dom": "17.0.2",
    "react-scripts": "4.0.3",
    "styled-components": "^5.3.3",
    "@usedapp/core": "^1.1.5",
    "@uniswap/sdk": "^3.0.2",
    "@uniswap/v2-core": "^1.0.1",
    "@uniswap/v2-periphery": "^1.1.0-beta.0",
    "ethers": "^5.7.0",
    "web3": "^4.5.0"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "scripts": {
    "build": "react-scripts build",
    "eject": "react-scripts eject",
    "ipfs": "yarn build && ipfs-deploy build/",
    "start": "react-scripts start",
    "test": "react-scripts test"
  }
}

Original Error Error after installing web3

javascript node.js web3js
1个回答
0
投票

首先,尝试更新您的节点版本。我相信您的问题与以前的节点版本与 OpenSSL 冲突有关。

npm install -g n

n latest

如果问题仍然存在,请更改

react-scripts start

react-scripts --openssl-legacy-provider start

在你的 package.json 文件中。

希望这个答案有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.