'Attempted import error: 'Wallet' is not exported from '@project-serum/anchor'

Trying to import the Wallet class but getting error in title at runtime. This issue apparently should have been fixed in v0.21+ but it doesn't appear to work in my codebase.

Using nextjs v11.0.1 and @project-serum/anchor v0.23.0

Relevant snippet from index.tsx

import { Provider, Program, Wallet } from '@project-serum/anchor';
import { Keypair } from '@solana/web3.js';

const Page = () => {
 const testWallet = new Wallet(Keypair.generate());
 return <div></div>;
}

The above snippet DOES work when using @project-serum/anchor v0.16.2

next.config.js

const path = require('path');
const withTM = require('next-transpile-modules')([
  '@blocto/sdk',
  '@project-serum/sol-wallet-adapter',
  '@solana/wallet-........,
]);

module.exports = withTM({
  target: 'serverless',
  distDir: 'build',
  trailingSlash: true,
  webpack5: false,
  webpack(config) {
    config.module.rules.push(
      {
        test: /\.svg$/,
        issuer: {
          test: /\.(js|ts)x?$/,
        },
        use: ['@svgr/webpack'],
      },
      {
        test: /\.png$/,
        issuer: {
          test: /\.(js|ts)x?$/,
        },
        use: ['file-loader'],
      },
      {
        test: /\.mjs$/,
        include: /node_modules/,
        type: 'javascript/auto',
      },
    );
    config.resolve.alias = {
      ...config.resolve.alias,
      assets: path.resolve(__dirname, './public/assets'),
    };
    config.node = {
      fs: 'empty',
    };

    return config;
  },
});

tsconfig.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./src",
    "module": "esnext",
    "paths": {
      "assets/*": ["./public/assets/*"],
      "@solana/*": ["./node_modules/@solana/*"]
    },
    "incremental": true
  },
  "exclude": ["node_modules"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}



Solution 1:[1]

Hmm, not exactly sure if this is the same issue I had, but you can try to just re-create the Wallet type locally and imports it.

Looks like this for me

export class MyWallet implements Wallet {

    constructor(readonly payer: Keypair) {
        this.payer = payer
    }

    async signTransaction(tx: Transaction): Promise<Transaction> {
        tx.partialSign(this.payer);
        return tx;
    }

    async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
        return txs.map((t) => {
            t.partialSign(this.payer);
            return t;
        });
    }

    get publicKey(): PublicKey {
        return this.payer.publicKey;
    }
}

Solution 2:[2]

I had to import Wallet like this in react

const { Wallet } = require("@project-serum/anchor");

Solution 3:[3]

Use NodeWallet instead of Wallet

import { Provider, Program, NodeWallet } from '@project-serum/anchor';

Solution 4:[4]

Using "@project-serum/anchor": "^0.14.0",

import * as anchor from '@project-serum/anchor';

anchor.web3.Keypair.generate().publicKey
anchor.web3.Keypair.generate().secretKey

Looks like, in that version that you are using, Wallet is not available and before, web3 was part of @project-serum/anchor. Then, they moved it into a separete package @solana/web3.js.

Solana dev tools reminds me of the beginning of solidity. Since it is a new and demanding technology, It changes rapidly

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 DaveTheAl
Solution 2 Zohab Ali
Solution 3 ismail bangee
Solution 4