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

huangapple go评论55阅读模式
英文:

cannot access ethers.js providers when using import statement in typescript

问题

以下是翻译好的部分:

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

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

它出现了以下错误

> TSError:  无法编译 TypeScript- 错误 TS2339类型 'typeof import' 上不存在属性 'providers'

以下代码可以正常工作

const ethers = require("ethers");

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

ethers.js 文档表示它可以在 es6 上运行是因为我使用了 ts-node 
英文:

I am running typescript using ts-node and try to use ethers.js provider:

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

it gets the following error:

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

The code works using:

const ethers = require("ethers");

but I need es6 format to run the script since my other module run on es6, otherwise I have to convert all other dependencies to use "require" statement, which is too much.

ethers.js doc says that it can run on es6. Is it because I use ts-node?

答案1

得分: 0

根据此 ethers.js 已关闭问题,此问题已得以解决。

英文:

according to this ethers.js closed issuethis is fixed by

executing:

const provider = new ethers.JsonRpcProvider();

instead

huangapple
  • 本文由 发表于 2023年3月7日 15:29:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定