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

部署含共享包的Firebase Functions时锁文件冲突报错求助

Firebase Functions单仓部署依赖同步失败问题解决

问题背景

单仓结构:

/front
/back
/shared

本地Firebase模拟器运行正常,但部署后端Functions时触发以下错误:

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
✔  functions: required API cloudfunctions.googleapis.com is enabled
✔  functions: required API cloudbuild.googleapis.com is enabled
✔  artifactregistry: required API artifactregistry.googleapis.com is enabled
i  functions: Loaded environment variables from .env.
i  functions: preparing functions directory for uploading...
i  functions: packaged $PROJECT_PATH/MYPROJECT/back/functions (2.39 MB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: creating Node.js 18 function api(us-central1)...
Build failed: npm ERR! code EUSAGE
npm ERR!
npm ERR! `npm ci` can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.
npm ERR!
npm ERR! Invalid: lock file's @MYPROJECT/shared@1.0.0 does not satisfy @MYPROJECT/shared@
npm ERR! Missing: @MYPROJECT/shared@ from lock file
npm ERR!
npm ERR! Clean install a project
npm ERR!
npm ERR! Usage:
npm ERR! npm ci
npm ERR!

相关配置

  • back/functions/package.json依赖:
"dependencies": {
    "@MYPROJECT/shared": "file:../../shared",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.1",
    "got": "^11.8.2",
    "openai": "^2.0.5",
    "serialize-error": "^11.0.0",
    "vite": "^4.0.4",
    "vitest": "^0.28.3"
  },
  • shared/package.json开发依赖:
"devDependencies": {
    "@types/jest": "^29.1.2",
    "@types/node": "^18.8.5",
    "@typescript-eslint/eslint-plugin": "^5.38.0",
    "@typescript-eslint/parser": "^5.38.0",
    "@vitest/coverage-c8": "^0.28.3",
    "eslint": "^8.23.1",
    "eslint-config-airbnb": "^19.0.4",
    "eslint-config-airbnb-typescript": "^17.0.0",
    "eslint-config-prettier": "^8.5.0",
    "eslint-plugin-import": "^2.26.0",
    "eslint-plugin-prettier": "^4.2.1",
    "prettier": "^2.7.1",
    "typescript": "^4.8.4",
    "vite": "^4.0.4",
    "vitest": "^0.28.3"
  }
}
  • back/firebase.json配置:
{
  "source": "functions",
  "functions": {
    "predeploy": "npm --prefix functions run build"
  }
}

已尝试操作

cd $PROJECT_PATH/MYPROJECT/shared
rm -r node_modules; rm package-lock.json; npm cache clean --force
cd $PROJECT_PATH/MYPROJECT/back/functions
rm -r node_modules; rm package-lock.json; npm cache clean --force
npm install
cd $PROJECT_PATH/MYPROJECT/shared
npm install
cd $PROJECT_PATH/MYPROJECT/back/
firebase functions:delete api; firebase deploy --only functions

解决建议

  • 补全shared包版本号
    检查shared/package.json是否存在version字段,设置为明确值(如"version": "1.0.0"),确保与lock文件中记录的版本一致。

  • 强制Firebase使用npm install替代npm ci
    修改back/firebase.json,添加npmInstall配置:

    {
      "source": "functions",
      "functions": {
        "predeploy": "npm --prefix functions run build",
        "npmInstall": "npm install"
      }
    }
    

    原因:npm ci严格依赖lock文件,而本地file依赖的路径在云端不存在,会触发同步错误;npm install会重新解析依赖,适配云端环境。

  • 打包shared为本地tgz包引用

    1. 在shared目录执行npm pack,生成myproject-shared-1.0.0.tgz(名称根据版本号变化)
    2. 修改back/functions/package.json中的依赖:
      "@MYPROJECT/shared": "file:../../shared/myproject-shared-1.0.0.tgz"
      
    3. 重新生成lock文件:cd back/functions && rm package-lock.json && npm install
  • 调整predeploy脚本顺序
    如果shared是TypeScript项目,确保先构建shared再构建functions,修改firebase.json的predeploy:

    "predeploy": "cd ../../shared && npm run build && cd ../back/functions && npm run build"
    
  • 配置根目录firebase.json支持单仓
    在项目根目录创建firebase.json,统一管理单仓的Functions配置:

    {
      "functions": [
        {
          "source": "back/functions",
          "codebase": "default",
          "predeploy": ["npm --prefix shared run build", "npm --prefix back/functions run build"],
          "npmInstall": "npm install"
        }
      ]
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 05:42:26