I create an instance of a Program in the following way and try to read accounts from the program instance.

const network = clusterApiUrl('devnet');
const connection = new Connection(network, "processed");
const provider = return new AnchorProvider(
    connection, window.solana, { preflightCommitment: "processed" },
const program = new Program<any>(idl, programID, provider)

Problem: If a user doesn't have Phantom wallet installed this would fail with the following error:

TypeError: Cannot read properties of undefined (reading ‘publicKey’)

Question: How can I properly instantiate a Program to read accounts when a user doesn't have Phantom installed?

1 Answer 1


I found a workaround. If the solana object is not found in window, passing a random public key solves the problem.

const wallet = window.solana ?? Keypair.generate();
return new AnchorProvider(
    connection, wallet, { preflightCommitment: "processed" },

Your Answer

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

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