You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用esrun运行TypeScript文件时遭遇ERR_MODULE_NOT_FOUND错误求助

问题:esrun执行TypeScript文件时出现ERR_MODULE_NOT_FOUND错误

错误信息

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\Diogo Barros\Documents\projects\spl-token\node_modules.bin\node_modules@solana-developers\helpers\dist\index.js' imported from 'C:\Users\Diogo Barros\Documents\projects\spl-token\node_modules.bin\esrun-1717410142669.tmp.mjs'

正确导入路径

'C:\Users\Diogo Barros\Documents\projects\spl-token\node_modules\@solana-developers\helpers\dist\index.js'

我的代码

import { createMint, getAssociatedTokenAddress, getOrCreateAssociatedTokenAccount } from "@solana/spl-token";
import dotenv from "dotenv";
dotenv.config()
import {
  getKeypairFromEnvironment,
  getExplorerLink,
} from "@solana-developers/helpers";
import { Connection,PublicKey, clusterApiUrl } from "@solana/web3.js";

const connection = new Connection(clusterApiUrl("devnet"));

const user = getKeypairFromEnvironment("SECRET_KEY");

console.log(
  `🔑 Loaded our keypair securely, using an env file! Our public key is: ${user.publicKey.toBase58()}`
);

const tokenMintAccount = new PublicKey(
    "XyCbpUa4uzmVD5KLAHUujV73X81ErvfhdWFCLWa5v7N"
);
// Here we are making an associated token account for our own address, but we can 
// make an ATA on any other wallet in devnet!
// const recipient = new PublicKey("SOMEONE_ELSES_DEVNET_ADDRESS");
const recipient = user.publicKey;

const tokenAccount = await getOrCreateAssociatedTokenAccount(
    connection,
    user,
    tokenMintAccount,
    recipient
);

console.log(`Token Account: ${tokenAccount.address.toBase58()}`);

const link = getExplorerLink(
  "address",
  tokenAccount.address.toBase58(),
  "devnet"
);

console.log(`✅ Created token Account: ${link}`);

已尝试操作

  • 删除node_modules和package-lock.json后重新执行npm install,问题仍未解决

可行解决方案尝试

1. 替换esrun为tsx

esrun存在路径解析bug的概率较高,直接改用tsx执行文件:

npx tsx your-file-name.ts

长期使用可在package.json中配置脚本:

"scripts": {
  "start": "tsx your-file-name.ts"
}

之后执行npm start即可。

2. 调整esrun版本

当前esrun版本可能存在路径解析问题,尝试升级到最新版:

npm install esrun@latest --save-dev

若升级无效,尝试降级到稳定版本(如esrun@0.14.1):

npm install esrun@0.14.1 --save-dev

3. 配置tsconfig指定模块路径

在项目根目录创建/修改tsconfig.json,明确模块解析规则:

{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "@solana-developers/helpers": ["node_modules/@solana-developers/helpers/dist/index.js"]
    }
  }
}

执行时指定该配置文件:

esrun --tsconfig tsconfig.json your-file-name.ts

4. 清理npm缓存后重装依赖

缓存异常可能导致路径错误,执行以下命令:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

5. 改用ts-node执行

如果以上方法均无效,直接使用ts-node:

npx ts-node your-file-name.ts

内容的提问来源于stack exchange,提问作者Diogo Barros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 19:42:34