solana-program-library: Error to import `@solana/spl-token` ESM module

Importing @solana/spl-token occurs errors like below such as next.js or browser.

error - ../node_modules/.pnpm/@solana+spl-token@0.3.1/node_modules/@solana/spl-token/lib/esm/extensions/extensionType.mjs:5:0
Module not found: Can't resolve './memoTransfer.mjs'

Import trace for requested module:
../node_modules/.pnpm/@solana+spl-token@0.3.1/node_modules/@solana/spl-token/lib/esm/extensions/index.mjs
../node_modules/.pnpm/@solana+spl-token@0.3.1/node_modules/@solana/spl-token/lib/esm/index.mjs
./src/pages/index.tsx

It seems to need that some ts files in src would be updated like below.

diff --git a/src/extensions/extensionType.ts b/src/extensions/extensionType.ts
index cbc0f4760f6c325d092d5d4b0441046b93b1d0f9..e4b4a3aa267b26cfe83b02292bd74219434343a5 100644
--- a/src/extensions/extensionType.ts
+++ b/src/extensions/extensionType.ts
@@ -2,11 +2,11 @@ import { ACCOUNT_SIZE } from '../state/account';
 import { Mint, MINT_SIZE } from '../state/mint';
 import { MULTISIG_SIZE } from '../state/multisig';
 import { ACCOUNT_TYPE_SIZE } from './accountType';
-import { MEMO_TRANSFER_SIZE } from './memoTransfer';
-import { DEFAULT_ACCOUNT_STATE_SIZE } from './defaultAccountState';
+import { MEMO_TRANSFER_SIZE } from './memoTransfer/index';
+import { DEFAULT_ACCOUNT_STATE_SIZE } from './defaultAccountState/index';
 import { MINT_CLOSE_AUTHORITY_SIZE } from './mintCloseAuthority';
 import { IMMUTABLE_OWNER_SIZE } from './immutableOwner';
-import { TRANSFER_FEE_CONFIG_SIZE, TRANSFER_FEE_AMOUNT_SIZE } from './transferFee';
+import { TRANSFER_FEE_CONFIG_SIZE, TRANSFER_FEE_AMOUNT_SIZE } from './transferFee/index';
 import { NON_TRANSFERABLE_SIZE } from './nonTransferable';
 import { INTEREST_BEARING_MINT_CONFIG_STATE_SIZE } from './interestBearingMint/state';
 
diff --git a/src/extensions/index.ts b/src/extensions/index.ts
index 3803bb7d9e28a54a458f24be156eebe6f28cd7e9..6aea395449cda46555741dad2f8393aa6915fcdb 100644
--- a/src/extensions/index.ts
+++ b/src/extensions/index.ts
@@ -4,6 +4,6 @@ export * from './extensionType';
 export * from './memoTransfer/index';
 export * from './mintCloseAuthority';
 export * from './immutableOwner';
-export * from './interestBearingMint';
+export * from './interestBearingMint/index';
 export * from './nonTransferable';
 export * from './transferFee/index';
diff --git a/src/extensions/interestBearingMint/actions.ts b/src/extensions/interestBearingMint/actions.ts
index 552aadfafa0e2b5f079a0e50b7dfe0c8fecd155d..f6348d8b43c297db6d845efe9bbb1ca5997a80b7 100644
--- a/src/extensions/interestBearingMint/actions.ts
+++ b/src/extensions/interestBearingMint/actions.ts
@@ -10,7 +10,7 @@ import {
 } from '@solana/web3.js';
 import { getSigners } from '../../actions/internal';
 import { TOKEN_2022_PROGRAM_ID } from '../../constants';
-import { createInitializeMintInstruction } from '../../instructions';
+import { createInitializeMintInstruction } from '../../instructions/index';
 import { ExtensionType, getMintLen } from '../extensionType';
 import {
     createInitializeInterestBearingMintInstruction,
diff --git a/src/extensions/interestBearingMint/instructions.ts b/src/extensions/interestBearingMint/instructions.ts
index ca330ce5f74a942f2d5c68147e29b1c178cba267..e1a393a34690597460fa57b1d66eeaf8116e01af 100644
--- a/src/extensions/interestBearingMint/instructions.ts
+++ b/src/extensions/interestBearingMint/instructions.ts
@@ -2,7 +2,7 @@ import { struct, s16, u8 } from '@solana/buffer-layout';
 import { publicKey } from '@solana/buffer-layout-utils';
 import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
 import { TOKEN_2022_PROGRAM_ID } from '../../constants';
-import { TokenInstruction } from '../../instructions';
+import { TokenInstruction } from '../../instructions/index';
 import { addSigners } from '../../instructions/internal';
 
 export enum InterestBearingMintInstruction {

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 15 (6 by maintainers)

Most upvoted comments

@jordansexton

It works! Thanks for your quick action!

Following up here: https://github.com/solana-labs/wallet-adapter/issues/532#issuecomment-1221591201

@solana/spl-token uses a similar build config to @solana/wallet-adapter.

This is something having to do with how Next.js is handling ESM imports.

Getting same issue