Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why is fs.readFile returning void in node.js

I’m new to node,js,typescript and NPM as a whole, I need a certain funtionality in a discord bot I’m building and only sdk I found to get it is in typescript which is a whole new world for me,
I’m trying to read a file using fs.readFile
here is the code:

import { readFile } from "fs";
import { Connection, Keypair } from "@solana/web3.js";
import { getOrca, OrcaFarmConfig, OrcaPoolConfig } from "@orca-so/sdk";
import Decimal from "decimal.js";

//console.log('test');
const main = async() => {
    const secretKeyString = readFile("lp.json", (error, data) => data);
    const secretKey = Uint8Array.from(JSON.parse(secretKeyString));
    const owner = Keypair.fromSecretKey(secretKey);
}

I’m getting a problem:

Argument of type 'void' is not assignable to parameter of type 'string'.

ik that the function is async but I’m not familliar with asyncio in javascript so how can I resolve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should use readFileSync here as it will serve the purpose you need in that context.

import { readFile } from "fs";
import { Connection, Keypair } from "@solana/web3.js";
import { getOrca, OrcaFarmConfig, OrcaPoolConfig } from "@orca-so/sdk";
import Decimal from "decimal.js";

//console.log('test');
const main = async() => {
    const secretKeyString = readFileSync("lp.json", {
        encoding: "utf-8",
    });
    const secretKey = Uint8Array.from(JSON.parse(secretKeyString));
    const owner = Keypair.fromSecretKey(secretKey);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading