When utilizing an import statement in TypeScript, it is not possible to access ethers.js providers

While trying to utilize ethers.js provider in my TypeScript project using ts-node, I encountered the following issue:

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

The error message displayed was:

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

Switching to the following code made it work:

const ethers = require("ethers");

However, I prefer using the es6 format for consistency with other modules. If I stick to the es6 format, I would have to convert all other dependencies to use the "require" statement, which seems like a daunting task.

According to the ethers.js documentation, it should be compatible with es6 syntax. Could this issue be caused by my use of ts-node?

Answer №1

Based on information from this closed ethers.js issue, the solution is to:

do the following:

const provider = new ethers.JsonRpcProvider();

as an alternative.

Answer №2

Exploring the Changes in FreeCodeCamp Solidity Course

With the introduction of Ethers v6, a significant alteration was made where ethers.provider* got replaced by ethers.*:

One notable change to bear in mind is that all ethers.providers.* have now been transitioned to ethers.*, along with the renaming of Web3Provider to BrowserProvider. This new design aims to wrap EIP-1193 providers, aligning with the standards adopted by modern Web3.js and injected providers.

For further information on this topic, check out the GitHub issue: https://github.com/ethers-io/ethers.js/issues/3794

To learn more about the migration process, refer to the guide provided here:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error TS2339: Property does not exist on type 'object' - Typescript arrow function issue

In my experience with Angular, I have noticed that I encounter typescript compile errors quite often when using fat arrow functions within an rxjs stream. Despite being able to run the app and having it transpile successfully, I am curious about how to re ...

Tips for setting a default value for the local file in React Native

I am attempting to assign a default value to my Avatar component in React Native Elements as I do not have all the required icon files prepared. However, when I use the OR logic, it does not seem to work as expected. What is the correct approach to achie ...

Deriving variable function parameters as object or tuple type in TypeScript

Searching for a similar type structure: type ArgsType<F extends Function> = ... which translates to ArgsType<(n: number, s: string)=>void> will result in [number, string] or {n: number, s: string} Following one of the provided solu ...

Tips for inspecting and troubleshooting JavaScript code while working with Typescript

One feature I appreciate about Visual Studio 2017 is its ability to debug directly in TypeScript, which is often very useful. However, there are instances when debugging the underlying JavaScript code becomes necessary. Is there a way to instruct Visual St ...

Is it possible to loop through a subset of a collection using *ngFor?

Is it possible to iterate through a specific range of elements in a collection using *ngFor? For instance, I have a group of checkboxes with their form control name and label specified as follows: [{id: 'c1', label: 'C1'}, ...] Assum ...

A function that can handle either a generic data type or an array containing elements of the same data type

function process<Type>(input: Type | Type[]): Type { if (Array.isArray(input)) { // input here is definitely Type[] return input.map((element) => process(element) /* <- this error message is saying 'Type[]' is not the ...

Angular 2: A helpful guide on how to duplicate an object within another object

I'm seeking assistance on how to duplicate an object into another object using Angular 2. Previously in Angular, I utilized angular.copy() to duplicate objects to the vague reference of the original ones. However, when attempting the same in Angular ...

How to effectively utilize TypeScript in a team environment using both Atom and VSCode?

Our team utilizes TypeScript with both Atom and VSCode as our editors, but we are facing challenges with the tsconfig.json file. VSCode is not recognizing the typings, causing the namespace for 'ng' (for Angular 1.x) to be unknown in VSCode. Wh ...

Enhancing the getDate function in JavaScript with additional days

My function for selecting the date is working perfectly: formatDateField(event: Date, formControl: string) { this.form .get(formControl) .patchValue( this.datePipe.transform(event.getTime(), "yyyy-MM-dd'T'HH:mm:ss&quo ...

What are the steps to resolve warnings in an imported json file?

I am working on a Vue project where I have imported a JSON file into my TypeScript script using import jsonData from '@/assets/data1.json'; Although the data is accessible and functions correctly, I am encountering numerous warnings during the b ...

Learn how to dynamically pass a click event from a div to a checkbox with delegation

I need assistance with a scenario where clicking on a button within a component triggers another click event on a specific checkbox located in a different div within the same component. Essentially, I want to programmatically simulate a click on the checkb ...

Setting text in a datetime picker with ngx-mat-datetime-picker in an Angular application is a straightforward process

I've been utilizing the ngx-mat-datetime-picker library from angular-material-components to enable datetime selection. It's functioning effectively, but I'm searching for a way to insert text preceding the hour and minute inputs, such as &ap ...

What is the reason for user.id being set as a 'string' by default?

Currently, I am in the process of creating a Next Auth system using TypeScript, and I have encountered a type issue related to the 'user.id' data. This error is specifically happening in the callbacks section. The types of 'user.id' ar ...

Unable to make custom font work in TailwindCSS and ReactJS project

I have incorporated a custom font into my projects using React, TypeScript, TailWind, and NextJS. The font file is stored in the /fonts directory with the name Glimer-Regular.ttf. To implement the font, I added the following code snippet to my global.css ...

The Ionic framework has a defined variable

In my code, I have initialized a variable inside the constructor like this: constructor(public http: HttpClient) { this.data = null; this.http.get(this.url).subscribe((datas: any) => { this.dbUrl = datas[0].db_url2; console.log(this ...

What causes an Object to potentially be null even after Array.isArray(obj) check has been performed?

The provided code demonstrates TypeScript focusing on the line result[key].push(submenuKey); where it acknowledges that result might be null, but a check for Array.isArray() is included. Code snippet: interface resultI { [key: string]: string | null ...

Firebase Cloud Function Local Emulator Fails to Retrieve Data with Error 404

My goal is to locally trigger a Firebase Cloud Function using the emulator. However, every time I try, the function returns a 404 Not Found status code and a response body of Cannot Get. The function is deployed locally and visible on the UI, but it fails ...

I'm experiencing an issue with redirect in Nextjs that's causing an error message to appear. The error reads: "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I'm currently diving into the world of NextJS and working on creating a simple recipe application. Utilizing the new App Router has been smooth sailing for the most part, except for one hiccup with the login function. After successfully logging in (us ...

Validator returns undefined when expressing invalid data

Having an issue with validation, here is the code snippet: routes.js var express = require('express'); var router = express.Router(); var hello_controller = require('../api/controllers/helloController'); var { validationRules, validat ...

Error: "Cannot find module 'tsc' in TypeScript, Express, and React application during deployment on Heroku

Currently, I am working on developing an Express app using TypeScript and a React app bootstrapped with create-react-app in JavaScript. The project has a specific directory structure which can be viewed here. The server code is located within the server/sr ...