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

使用Bun构建的本地TypeScript包导入失败问题求助

问题:Bun+TypeScript工具包在Elysia项目运行时提示模块找不到

我用Bun+TypeScript+Mongoose创建了@warcayac/utils工具包,按教程完成配置后执行bun run build和bun link成功注册。工具包和Elysia项目的配置如下,编辑器能识别工具包中的MongoClient类,但执行测试文件时出现Cannot find module "@warcayac/utils/MongoClient"错误。

工具包配置

package.json

{"name": "@warcayac/utils","module": "index.ts","type": "module","main": "dist/index.js","types": "dist/index.d.ts","version": "0.0.1","description": "A collection of utilities for backend and frontend projects","scripts": {"build": "bun build --target=node ./src/index.ts --outfile=dist/index.js && bun run build:declaration","build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json","postbuild": "rimraf tsconfig.types.tsbuildinfo"},"devDependencies": {"bun-types": "latest","rimraf": "^5.0.5"},"peerDependencies": {"typescript": "^5.3.3"},"dependencies": {"mongoose": "^8.1.0"},"keywords": ["utils"],"author": "Me","license": "MIT","files": ["dist/*.js","dist/*.d.ts"]}

tsconfig.json

{"compilerOptions": {"lib": ["ESNext"],"target": "ESNext","moduleDetection": "force","jsx": "react-jsx","module": "ESNext","rootDir": "./src","baseUrl": "./","paths": {"@/*": ["src/*"]},"types": ["bun-types"],"moduleResolution": "bundler","allowImportingTsExtensions": true,"noEmit": true,"outDir": "./dist","downlevelIteration": true,"allowJs": true,"composite": true,"verbatimModuleSyntax": true,"forceConsistentCasingInFileNames": true,"allowSyntheticDefaultImports": true,"strict": true,"noFallthroughCasesInSwitch": true,"skipLibCheck": true},"include": ["src/**/*.ts"]}

Elysia项目配置

package.json

{"name": "notes_server","version": "1.0.50","scripts": {"test": "echo \"Error: no test specified\" && exit 1","dev": "bun run --watch src/index.ts","start": "bun run src/index.ts"},"dependencies": {"@elysiajs/cors": "^0.8.0","@elysiajs/static": "^0.8.1","@warcayac/utils": "link:@warcayac/utils","ansis": "^2.0.3","elysia": "latest","mongoose": "^8.1.0"},"devDependencies": {"bun-types": "latest"},"module": "src/index.js"}

tsconfig.json

{"compilerOptions": {"target": "ES2021","module": "ES2022","moduleResolution": "node","types": ["bun-types"],"esModuleInterop": true,"forceConsistentCasingInFileNames": true,"strict": true,"skipLibCheck": true}}

测试文件导入语句

import MongoClient from "@warcayac/utils/dist/mongodb/MongoClient";

解决方案

1. 修正工具包的打包命令与输出结构

当前工具包仅打包了src/index.ts到dist/index.js,子模块文件未被单独打包。修改package.json中的build命令,让Bun将整个src目录下的文件打包到dist对应子目录:

"scripts": {
  "build": "bun build --target=node ./src/**/*.ts --outdir=dist && bun run build:declaration",
  // ... 其他命令不变
}

2. 更新工具包的package.json配置

  • 扩展files字段,确保包含dist下的所有子目录文件:
    "files": ["dist/**/*"]
    
  • 添加exports和typesVersions字段,明确模块的导出路径(让Bun/Node能正确解析子路径导入并识别类型):
    "exports": {
      ".": "./dist/index.js",
      "./mongodb/MongoClient": "./dist/mongodb/MongoClient.js"
    },
    "typesVersions": {
      "*": {
        "mongodb/MongoClient": ["dist/mongodb/MongoClient.d.ts"]
      }
    }
    

3. 在工具包入口导出目标模块(可选,简化导入)

在src/index.ts中导出MongoClient,这样可以直接从包名导入:

export { default as MongoClient } from './mongodb/MongoClient';

4. 修正Elysia项目中的导入语句

如果用了步骤3的入口导出,直接从包名导入:

import { MongoClient } from "@warcayac/utils";

如果用子路径导入,去掉dist前缀:

import MongoClient from "@warcayac/utils/mongodb/MongoClient";

5. 重新构建并更新链接

在工具包目录执行:

bun run build
bun link

在Elysia项目目录重新链接(确保生效):

bun link @warcayac/utils

内容的提问来源于stack exchange,提问作者Ουιλιαμ Αρκευα

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:55:56