'Import web3 not found... in solana web3.js
I npm installed everything, yet only web3 is not working? Is this a bug or is there an alternative to this solution?
import React,{useEffect, useState}from 'react';
import { Connection, PublicKey,Account } from '@solana/web3.js';
import { MintLayout, TOKEN_PROGRAM_ID, Token } from '@solana/spl-token';
import { Program, Provider } from '@project-serum/anchor';
import { sendTransactions } from './connection';
import './CandyMachine.css';
Error message below
export 'web3' (imported as 'web3') was not found in '@solana/web3.js' (possible exports: Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction)
Solution 1:[1]
actually you dont have to call web3 because you already imported Connection, PublicKey, Account
directly.
example:
import { Connection, PublicKey } from '@solana/web3.js';
// Create new connection
const connection = new Connection(
web3.clusterApiUrl('devnet'),
'confirmed',
);
// Generate a new random public key
const somePublicKey = new PublicKey("someBase58AddressXxxXXXx2323")
if you want to call web3
, your import should could be look like below:
import * as web3 from '@solana/web3.js';
// Create new connection
const connection = new web3.Connection(
web3.clusterApiUrl('devnet'),
'confirmed',
);
// Generate a new random public key
const somePublicKey = new web3.PublicKey("someBase58AddressXxxXXXx2323")
// etc
Solution 2:[2]
Error is clear. @solana/web3.js
has no web3
to be exported. If you want to connect
import * as anchor from '@project-serum/anchor';
import { clusterApiUrl } from '@solana/web3.js';
const rpcHost =https://api.devnet.solana.com
const connection = new anchor.web3.Connection(rpcHost
? rpcHost
: anchor.web3.clusterApiUrl('devnet'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kafinsalim |
Solution 2 | Yilmaz |