Bun构建Hono应用后报错:找不到模块./lib/decorators/timed
Bun+Hono+WebSocket应用打包后启动报错
开发阶段运行完全正常,但执行bun build构建后启动产物时出现以下错误:
Cannot find module "./lib/decorators/timed" from "/Users/b/codebases/az/mdlr/api/.api/index.js"
我的package.json配置如下:
{ "scripts": { "api:local": "dotenv -e .env.local -- bun run --hot --watch index.ts", "api:prod": "dotenv -e .env.prod -- bun run --hot index.ts", "api:build": "dotenv -e .env.local -- bun build index.ts --outdir .api --target bun", "api:start": "bun run .api/index.js", "api:tsc": "tsc", "api:lint": "eslint . --ext .ts,.tsx", "api:lint:fix": "eslint . --ext .ts,.tsx --fix", "api:unimported": "unimported", "api:knip": "knip" }, "dependencies": { "cheerio": "^1.0.0-rc.12", "dotenv": "^16.4.5", "eslint": "^8.57.0", "firebase-admin": "^11.11.1", "hono": "^4.3.4", "moment": "^2.30.1", "mongoose": "^8.3.4", "slack-node": "^0.1.8", "stripe": "^14.23.0", "xml2js": "^0.6.2", "zod": "^3.23.8" }, "devDependencies": { "@types/bun": "^1.1.3", "@types/slack-node": "^0.1.7", "@types/stripe": "^8.0.417", "@types/xml2js": "^0.4.14", "@typescript-eslint/eslint-plugin": "^7.10.0", "@typescript-eslint/parser": "^7.10.0", "dotenv-cli": "^4.0.0", "knip": "^5.16.0", "unimported": "^1.31.1" } }
已尝试但无效的操作:
- 清理所有文件后重新执行
bun install安装依赖 - 修改文件夹名称等相关调整
可能的解决方向:
- 检查路径大小写一致性:MacOS开发环境默认大小写不敏感,但构建后的产物运行时路径匹配会严格区分大小写,确认代码中导入的
./lib/decorators/timed对应的实际文件路径(比如是否存在大小写差异,如Timed.ts) - 验证Bun构建产物完整性:查看
.api目录下是否生成了lib/decorators/timed对应的文件,如果缺失,说明该文件未被Bun打包,可能是导入方式(如动态导入未正确处理)或树摇规则导致,可尝试在构建命令中添加--no-tree-shaking禁用树摇测试 - 检查TypeScript路径配置:如果项目中有
tsconfig.json的路径映射配置,确认Bun构建是否能识别这些映射,可尝试在bun build命令中添加--tsconfig ./tsconfig.json明确指定配置文件 - 升级Bun版本:当前使用的
@types/bun是1.1.3,尝试执行bun upgrade升级到最新稳定版,可能修复构建时的路径解析bug - 确认导入语句正确性:检查所有引用
./lib/decorators/timed的代码,确认相对路径拼写无误,文件扩展名是否正确(比如是否遗漏.ts,开发时Bun会自动补全,构建时可能需要明确)
内容的提问来源于stack exchange,提问作者benxha
相关产品推荐
相关产品推荐

