3

I keep getting this error when trying to import fs to get my idl: Module not found: Can't resolve 'fs'

Code:

const { readFileSync } = require("fs"); 

export class BigBazaar {
  constructor(
    readonly connection: Connection,
    readonly anchorWallet: AnchorWallet,
    readonly userKeypair = (anchorWallet as NodeWallet).payer,
    readonly userPubKey: PublicKey | null = anchorWallet.publicKey,
    readonly provider = setProvider(
      new AnchorProvider(connection, anchorWallet, {})
    ),
    readonly idl: Idl = JSON.parse(
      readFileSync("../idl/BigBazaar_IDL_Devnet.json", "utf-8")
    ),
    readonly programId: string = idl.metadata.address,
    readonly program = new Program(idl, programId)
  ) {}
2
  • Are you running your code in the browser?
    – C.OG
    Commented Aug 12, 2022 at 18:55
  • @C.OG yeha i am. Ive tried changing the next config and getStaticProps and they didn't work, or I did them wrong
    – incog
    Commented Aug 12, 2022 at 19:18

1 Answer 1

3

The fs module doesn't exist in the browser, it's a built-in node module for reading from the file system.

Instead, try to import the file.

import idl from "../idl/BigBazaar_IDL_Devnet.json";

export class BigBazaar {
  constructor(
    readonly connection: Connection,
    readonly anchorWallet: AnchorWallet,
    readonly userKeypair = (anchorWallet as NodeWallet).payer,
    readonly userPubKey: PublicKey | null = anchorWallet.publicKey,
    readonly provider = setProvider(
      new AnchorProvider(connection, anchorWallet, {})
    ),
    readonly idl: Idl = idl,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.