Cannot read properties of undefined (reading 'toHexString') in ether.js - javascript

Please don't judge, i've no idea what to do and how to do :)
My code:
ether.js ( ^5.6.0)
import { ThirdwebSDK } from '#3rdweb/sdk'
import { ethers } from 'ethers'
const sdk = new ThirdwebSDK(
new ethers.Wallet(
process.env.METAMASK_PRIVATE_KEY,
ethers.getDefaultProvider(
'https://rinkeby.infura.io/v3/'
)
)
)
bug

If you are using JavaScript and getting this error then You made a mistake while importing dotenv. you are using ".config" as property . Use it like a method like following
require("dotenv").config()
It will work.

Another problem you might have is where your .env is located. Make sure that your .env is in the same folder as your code.

You most likely messed up env variable. Check if METAMASK_PRIVATE_KEY is the correct name. Because otherwise it will throw undefined and one of the ethersjs library will try to run hexToString() method on undefined value, thus you get the error.
EDIT:
You might as well forget to include this in your code:
import {} from 'dotenv/config'
// or if its not ESmodule
require('dotenv').config()
Absence of this import will throw undefined as well when you access env variable.

I had this problem aswell. I used a constructor for my sol contract that looked as following:
constructor(
address vrfCoordinatorV2,
uint256 entranceFee,
bytes32 gasLane,
uint64 subscriptionId,
uint32 callbackGasLimit,
uint256 interval
)
After alot of frustration i found out that my arguments for the deploy script wasn't lined up in the same order. This was causing the error:
"Cannot read properties of undefined (reading 'toHexString')"
In my case the arguments needed to be lined up as following:
const args = [
vrfCoordinatorV2Address,
networkConfig[chainId]["raffleEntranceFee"],
networkConfig[chainId]["gasLane"],
subscriptionId,
networkConfig[chainId]["callbackGasLimit"],
networkConfig[chainId]["keepersUpdateInterval"],
];

Another option is that you've edited the .env file and forgotten to save it. Hence the script won't see the new entry.

I had a similar issue, I actually missed saving an updated .env file. When I saved the updated .env properly this worked.

Related

TypeError: (0 , immer__WEBPACK_IMPORTED_MODULE_2__.default) is not a function

I am using this library:
redux-toolkit, and, as far as I understand, immer library as a part of it. I am trying to start a server and when it renders, I get this error
TypeError: (0 , immer__WEBPACK_IMPORTED_MODULE_2__.default) is not a function
It then refers to this line in file #reduxjs/toolkit/src/index.ts saying it is not a function:
enableES5().
EnableES5 is imported above like this: import { enableES5 } from 'immer'
How do I fix it? I tried changing the index.ts file, but since its a dependency I am not even sure I can change anything there.
Thank you

How do I use TypeScript typing information from a Node.js package?

I've read a ton of different variations of this same question, but I just can't wrap my mind around it. I'm using the websocket module (which I've used many times before without TypeScript) and I can't figure out how to type my variables?
I have a function that takes in a WebSocketConnection object, but when I give it the type WebSocketConnection I get a "Cannot find name 'WebSocketConnection'" from TypeScript.
I've installed the #types/websocket package and I can see the index.d.ts file with all the type definitions in it located in ./node_modules/#types/websocket/index.d.ts relative to my tsconfig.json file...
I've tried adding the "typeRoots" option to the tsconfig.json file, as well as "types". I've tried many combination of values but as far as I can tell leaving them off entirely is a better bet, so I've tried that as well. I've also tried many variations of importing data from both the .d.ts file and also the package itself, with no luck of course.
I tried using /// <reference path="node_modules/#types/websocket/index.d.ts" /> also with no luck.
I looked in the .d.ts file and found a very clear declaration of an interface called IStringified that looked like this:
export interface IStringified {
toString: (...args: any[]) => string;
}
So I tried to access IStringified and I'm still getting the same "Cannot find name 'IStringified'" error.
I'm probably just being really dumb and missing something plainly obvious, but any pointers or advice would be much appreciated! What in the world am I doing wrong?
Types of installed packages are not available globally. They must be imported into each file in order to use them. Don't mess with typeroots or triple slash directives at all, that will only make things worse.
In this particular case, the module exports a connection class, which is probably what you want. It's unfortunate name means to make it it look the class constructor that it really is you should probably rename in on import:
import {
server as WebSocketServer,
connection as WebSocketConnection,
} from 'websocket'
And now you can do:
const wsServer = new WebSocketServer({ httpServer: server });
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
doSomethingWithWsConnection(connection) // works
});
function doSomethingWithWsConnection(connection: WebSocketConnection) {
//...
}
Typesafe example on TS playground
So I tried to access IStringified and I'm still getting the same "Cannot find name 'IStringified'" error.
You import the type, then use the type:
import { IStringified } from 'websocket'
const foo: IStringified = { toString: () => 'asdf' }
Playground

New eslint errors with the #typescript-eslint/no-unsafe-* rules

I'm having some trouble with adding them into some existing projects. For example, I have a class in a module that I developed:
export default class ClassName {
// Class members
}
Now I import that into another project:
import ClassName from 'modulename';
const object = new ClassName();
I get 2 errors on this line.
On the object in const object:
error Unsafe assignment of an any value #typescript-eslint/no-unsafe-assignment
On the new in new ClassName:
error Unsafe construction of an any type value #typescript-eslint/no-unsafe-call
How can I avoid these errors?! I would really like to be able to follow these rules because I think they'd be so useful!
Thanks.
Here's another example:
import { readJsonSync } from 'fs-extra';
const testEnv = readJsonSync(testEnvPath);
Here I get the no-unsafe-assignment error on the testEnv of const testEnv, and the no-unsafe-call error on the readJsonSync call on the second line.
I can get rid of the first one with this code:
interface ITestEnv {
// interface members
}
const testEnv: ITestEnv = readJsonSync(testEnvPath) as ITestEnv;
however, I still can't figure out how to get rid of the second one on the readJsonSync call.
ESlint can not resolve the absolute import of your module,
which makes it to infer your class type as any.
Make sure baseUrl and paths in tsconfig.json file used by ESlint are defined correctly.
#see Typescript – Module Resolution
In the first case, you have just one error coming from the constructor, which is cascading to the const assignment. Something in your class implementation is making the type-inference to be inferred as any.
Not saying your code is incorrect. It might be, but there's an (open issue on Github) reporting a constructor as being incorrectly flagged by the same rule.
On your second issue, have you added #type/fs-extra as a project dependency? Many npm packages do not have types themselves. Types are created by someone and added to the #types library. When that's the case, the #types/package_name must be added as a dependency separately.

ES6-Importing method from one file to another gives error

I have an API utils file React JS where i have list of endpoints declared like below-
File1.js-
export const api_endpoints = {
api1: ()=> '../data/user'
}
I want api1() from File1.js in my another file (File2.js) hence i am importing this like below-
import {api_endpoints} from '../File1';
const myObj = {
[api_endpoints.api1()]: '../data/user2'
}
This gives me error in File2 saying-
"cannot read property api1 of undefined"
I am not sure why api_endpoints comes as undefined in File2.js. The paths are correct. Can someone help me understand that.
Looks like you are importing from wrong path. Tried the above example and it is working fine for me..
https://codesandbox.io/s/objective-meninsky-7gbch?file=/src/File2.js
You might have just copied it wrong, but you imported {api1_endpoints}, but are asking for api_endpoints

Path Error still the same after replacing path with constant (Module does not exist in the module map)

I have the following problem, I get:
"Unable to resolve module nik/env from /some/path/constant.js : Module does not exist in the module map or in these directories /path/to/project/root"
In my constant.js I do try to do the following:
import {
API_URL,
} from 'nik/env'
so I am basically just trying to get a variable (API_URL) out of my env.
However, when I tested this and I replaced the above with a hardcoded variable like so: const API_URL = 'http://my-api.myserver.com';, I still get the same error. Namely, that it cannot resolve nik/env - but there is no reference to nik/env in that file anymore.
Why could this be? Thanks a lot in advance!!

Categories

Resources