typescript - 在 typescript 中使用 import 语句时无法访问 ethers.js 提供程序

标签 typescript ethers.js ts-node

我正在使用 ts-node 运行 typescript 并尝试使用 ethers.js 提供程序:

import { ethers } from "ethers";
const provider = new ethers.providers.JsonRpcProvider(url);

出现以下错误:

TSError: ⨯ Unable to compile TypeScript: - error TS2339: Property 'providers' does not exist on type 'typeof import

代码的工作原理是:

const ethers = require("ethers");

但是我需要 es6 格式来运行脚本,因为我的其他模块在 es6 上运行,否则我必须将所有其他依赖项转换为使用“require”语句,这太多了。

ethers.js 文档说它可以在 es6 上运行。是因为我使用了ts-node吗?

最佳答案

根据this ethers.js closed issue这是通过修复的

执行:

const provider = new ethers.JsonRpcProvider();

相反

关于typescript - 在 typescript 中使用 import 语句时无法访问 ethers.js 提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75659064/

相关文章:

TypeScript 源码解析 : Get class decorator names

javascript - 使用 Typescript 我可以以更紧凑的方式创建服务吗?

npm - typechain --target ethers-v5 不被识别为内部命令

javascript - 在 ethers.js 中设置合约方法的气体限制

javascript - 在智能合约部署脚本中解构为 {getNamedAccounts, devployments, getChainId } 的对象是什么?

javascript - TS 节点:找不到源文件

javascript - React setState 更新不正确的子组件

typescript - 如何使用 TypeScript 和 mocha 进行异步/等待测试

javascript - 内部/模块/cjs/loader.js :800 throw err