I’m currently working on a TypeScript project using the Solana Web3.js library and encountered the following error when trying to run my script:
PS D:\Code\ts> npx esrun t1.tsnode:internal/process/esm_loader:34 internalBinding('errors').triggerUncaughtException( ^Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'D:\Code\ts\node_modules\.bin\node_modules\@solana\web3.js\lib\index.cjs.js' imported from D:\Code\ts\node_modules\.bin\esrun-1730469336690.tmp.mjsDid you mean to import "../@solana/web3.js/lib/index.cjs.js"? at finalizeResolution (node:internal/modules/esm/resolve:264:11) at moduleResolve (node:internal/modules/esm/resolve:924:10) at defaultResolve (node:internal/modules/esm/resolve:1148:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12) at ModuleLoader.resolve (node:internal/modules/esm/loader:359:25) at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39) at link (node:internal/modules/esm/module_job:86:36) { code: 'ERR_MODULE_NOT_FOUND', url: 'file:///D:/Code/ts/node_modules/.bin/node_modules/@solana/web3.js/lib/index.cjs.js'}Node.js v21.7.3
Code Snippet:
import { Connection, Transaction, SystemProgram, sendAndConfirmTransaction, PublicKey,} from "@solana/web3.js";require('dotenv').config();import { getKeypairFromEnvironment } from "@solana-developers/helpers";const suppliedToPubkey = process.argv[2] || null;if (!suppliedToPubkey) { console.log(`Please provide a public key to send to`); process.exit(1);}const senderKeypair = getKeypairFromEnvironment("SECRET_KEY");console.log(`suppliedToPubkey: ${suppliedToPubkey}`);const toPubkey = new PublicKey(suppliedToPubkey);const connection = new Connection("https://api.devnet.solana.com", "confirmed");console.log( `✅ Loaded our own keypair, the destination public key, and connected to Solana`,);const transaction = new Transaction();const LAMPORTS_TO_SEND = 5000;const sendSolInstruction = SystemProgram.transfer({ fromPubkey: senderKeypair.publicKey, toPubkey, lamports: LAMPORTS_TO_SEND,});transaction.add(sendSolInstruction);const signature = await sendAndConfirmTransaction(connection, transaction, [ senderKeypair,]);console.log( `💸 Finished! Sent ${LAMPORTS_TO_SEND} to the address ${toPubkey}. `,);console.log(`Transaction signature is ${signature}!`);```