症状
esbuild 因 Could not resolve 中止并指出某个模块说明符。把构建从其它打包器迁移过来、而 esbuild 并不读取其配置时尤为常见。
诊断
记录该说明符并验证可解析性:
node -e "console.log(require.resolve('react'))"
判断说明符是包、相对路径还是 Node 内置模块。
修复
安装依赖并配置解析:
import { build } from 'esbuild';
await build({
entryPoints: ['src/index.ts'],
bundle: true,
outfile: 'dist/bundle.js',
alias: { '@': './src' },
external: ['fs', 'path'], // Node built-ins for a server build
});
验证
检查打包产物;不再有 “Could not resolve” 错误。构建成功,生成的 bundle 运行时也不会因缺失导入而报错。