Skip to content

reason: 'could not detect network', code: 'NETWORK_ERROR', event: 'noNetwork' #5006

Open
@Kiki-Q

Description

@Kiki-Q

Ethers Version

5.6.9

Search Terms

No response

Describe the Problem

it's my first day to learn etherjs, I try the whole day,but I can't connect blockchain.I don't know what's the problem

import { ethers } from "ethers";

const mainUrl = "https://mainnet.infura.io/v3/my_infura_API_KEY ";

const providerETH = new ethers.providers.JsonRpcProvider(mainUrl);

const main = async () => {
    try {
        // 检查主网连接
        await providerETH.detectNetwork();
        const balance = await providerETH.getBalance(`vitalik.eth`);
        console.log(`主网余额: ${ethers.utils.formatEther(balance)} ETH`);
    } catch (error) {
        console.error("主网连接失败:", error.message);
    }
};
main();

I use terminal to run (node thisFile.js), this is error message:

 var error = new Error(message);
                    ^

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
    at Logger.makeError (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\logger\lib\index.js:233:21)
    at Logger.throwError (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\logger\lib\index.js:242:20)
    at JsonRpcProvider.<anonymous> (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:561:54)
    at step (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  reason: 'could not detect network',
  code: 'NETWORK_ERROR',
  event: 'noNetwork'
}

Node.js v24.1.0

this is my ethers version:"ethers": "^5.6.9"

BUT,when I copy the demo code from github,it alse has this error,does it mean some envoriment should done before I use this library?somebody can help me ?
this is github demo code:

// 导入ethers包
import { ethers } from "ethers";
// playcode免费版不能安装ethers,用这条命令,需要从网络上import包(把上面这行注释掉)
// import { ethers } from "https://cdn-cors.ethers.io/lib/ethers-5.6.9.esm.min.js";

// 利用公共rpc节点连接以太坊网络
// 可以在 https://chainlist.org 上找到
const ALCHEMY_MAINNET_URL = 'https://rpc.ankr.com/eth';
const ALCHEMY_SEPOLIA_URL = 'https://rpc.sepolia.org';
// 连接以太坊主网 - 修改为v5的写法
const providerETH = new ethers.providers.JsonRpcProvider(ALCHEMY_MAINNET_URL);
// 连接Sepolia测试网 - 修改为v5的写法
const providerSepolia = new ethers.providers.JsonRpcProvider(ALCHEMY_SEPOLIA_URL);

const main = async () => {
    // 利用provider读取链上信息
    // 1. 查询vitalik在主网和Sepolia测试网的ETH余额
    console.log("1. 查询vitalik在主网和Sepolia测试网的ETH余额");
    const balance = await providerETH.getBalance(`vitalik.eth`);
    // 修正Sepolia测试网地址(原地址多了两位)
    const balanceSepolia = await providerSepolia.getBalance(`0xd8dA6BF26964aF9D7eEd9e03E53415D37aA960`);
    // 将余额输出在console(主网)
    console.log(`ETH Balance of vitalik: ${ethers.formatEther(balance)} ETH`);
    // 输出Sepolia测试网ETH余额
    console.log(`Sepolia ETH Balance of vitalik: ${ethers.formatEther(balanceSepolia)} ETH`);
    
    // 2. 查询provider连接到了哪条链
    console.log("\n2. 查询provider连接到了哪条链");
    const network = await providerETH.getNetwork();
    console.log(network);

    // 3. 查询区块高度
    console.log("\n3. 查询区块高度");
    const blockNumber = await providerETH.getBlockNumber();
    console.log(blockNumber);

    // 4. 查询 vitalik 钱包历史交易次数
    console.log("\n4. 查询 vitalik 钱包历史交易次数");
    const txCount = await providerETH.getTransactionCount("vitalik.eth");
    console.log(txCount);

    // 5. 查询当前建议的gas设置
    console.log("\n5. 查询当前建议的gas设置");
    const feeData = await providerETH.getFeeData();
    console.log(feeData);

    // 6. 查询区块信息
    console.log("\n6. 查询区块信息");
    const block = await providerETH.getBlock(0);
    console.log(block);

    // 7. 给定合约地址查询合约bytecode,例子用的WETH地址
    console.log("\n7. 给定合约地址查询合约bytecode,例子用的WETH地址");
    // 修正WETH地址(原地址多了一位)
    const code = await providerETH.getCode("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2");
    console.log(code);
};

main();

& the same error message

var error = new Error(message);
                    ^

Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.6.8)
    at Logger.makeError (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\logger\lib\index.js:233:21)
    at Logger.throwError (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\logger\lib\index.js:242:20)
    at JsonRpcProvider.<anonymous> (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:561:54)
    at step (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
    at Object.throw (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
    at rejected (E:\DQ_Own\CODE\web3\docs\etherjs&web3js\projects\ethers-tutorial\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5) {
  reason: 'could not detect network',
  code: 'NETWORK_ERROR',
  event: 'noNetwork'
}

Node.js v24.1.0

then,I also tried on local hardhat project using "ethers": "5.7.2" the error same
& the latest version(6.14.3) the error different,but I think it because the api changed
this is eror message

const providerETH = new ethers.providers.JsonRpcProvider(mainUrl);
                                         ^

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider')
    at file:///E:/DQ_Own/CODE/web3/docs/Hardhat/demo/contracts/etherTest.js:6:42
    at ModuleJob.run (node:internal/modules/esm/module_job:327:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:663:26)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:99:5)

Node.js v24.1.0

so how can I use ethers successfully??

Code Snippet

Contract ABI

Errors

reason: 'could not detect network',
code: 'NETWORK_ERROR',
event: 'noNetwork

Environment

node.js (v12 or newer)

Environment (Other)

windows 10, node 24.1.0,ethers 5.6.9

Metadata

Metadata

Assignees

Labels

investigateUnder investigation and may be a bug.v5Issues regarding legacy-v5

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions