Monorepo项目Bitbucket Pipeline构建Web应用时无法解析@WebApp/domain
解决Monorepo中Bitbucket Pipeline无法访问domain目录的构建错误
构建Web应用时出现错误:Module not found: Error: Can't resolve '@WebApp/domain' in '/opt/atlassian/pipelines/agent/build/web/src',核心原因是Pipeline的构建流程没有正确处理Monorepo中跨目录的本地包依赖。
项目结构
Consumer-frontend ├── domain ├── mobile ├── web ├── package-lock.json └── bitbucket-pipelines.yml
解决方案
方法1:在根目录执行npm install(推荐)
当前Pipeline在web目录内执行npm install,导致npm无法正确解析file:../domain的本地依赖链接。修改为在项目根目录安装依赖,确保所有本地包被正确处理:
修改后的Pipeline代码:
pipelines: branches: test-deploy-2: - step: name: Building the app image: node:18.14.2 script: - npm install - cd web - npm run build artifacts: - web/build/** - step: name: Deploying the app to S3 deployment: test script: - pipe: atlassian/aws-s3-deploy:0.2.4 variables: AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION S3_BUCKET: $S3_BUCKET ACL: "public-read" LOCAL_PATH: "./web/build"
注意:修正了artifacts路径,因为build产物实际在web/build目录下。
方法2:配置路径解析(TypeScript/JavaScript项目)
如果项目使用TypeScript,在web目录的tsconfig.json中添加路径映射,让编译器能识别@WebApp/domain的位置:
{ "compilerOptions": { "baseUrl": "./src", "paths": { "@WebApp/domain": ["../../domain/src"] // 根据domain源码实际路径调整 } } }
纯JavaScript项目则创建jsconfig.json,配置格式与上述一致。
方法3:确保domain目录有合法的package.json
检查domain目录下是否存在package.json,且name字段必须为@WebApp/domain,示例:
{ "name": "@WebApp/domain", "version": "0.1.0", "private": true, "main": "src/index.ts" // 指向domain的入口文件 }
方法4:手动链接本地包(备选)
如果根目录install仍有问题,可在Pipeline中手动链接domain包:
script: - cd domain - npm link - cd ../web - npm link @WebApp/domain - npm install - npm run build
内容的提问来源于stack exchange,提问作者Preetam
相关产品推荐
相关产品推荐

