Firebase Functions部署失败:本地依赖无package.json问题求助
部署Firebase Functions时遇到错误:无法从"../shared"安装,因为该目录不包含package.json文件。本地依赖loanshub-shared在本地打包运行正常,但云端部署失败,此前该流程可正常运行。已尝试删除shared和functions目录下的node_modules并重新安装,问题依旧。
使用环境版本:
- NodeJS 12.13.0
- NPM 6
- firebase-tool 7.16.1
functions的package.json文件:
{ "name": "functions", "scripts": { "lint": "tslint --project tsconfig.json", "pack:loanshub-shared": "npm pack file:../shared", "update-models": "npm run pack:loanshub-shared && npm install loanshub-shared -force", "build": "tsc && npm run pack:loanshub-shared", "serve": "npm run build && firebase serve --only functions", "shell": "npm run build && firebase functions:shell", "start": "npm run shell", "logs": "firebase functions:log", "tslint-check": "tslint-config-prettier-check ./tslint.json", "deploy-backend": "firebase deploy --only functions,firestore:rules,storage:rules,database:rules && firebase use preview", "deploy:preview": "firebase use preview && npm run deploy-backend", "deploy:production": "firebase use production && npm run deploy-backend", "emulate": "firebase use preview && npm run get-config && firebase functions:shell && firebase emulators:exec \"./tests/test.sh\"", "get-config": "firebase use preview && firebase functions:config:get > .runtimeconfig.json", "test": "mocha -r ts-node/register tests/**/**.spec.ts" }, "main": "lib/index.js", "dependencies": { "apollo-boost": "^0.4.9", "apollo-link-context": "^1.0.20", "axios": "^0.26.1", "chai-as-promised": "^7.1.1", "cross-fetch": "^3.0.6", "firebase": "^7.16.2", "firebase-admin": "^9.11.0", "firebase-functions": "3.14.1", "graphql": "^15.4.0", "graphql-tag": "^2.11.0", "loanshub-shared": "file:loanshub-shared-1.0.0.tgz", "lodash": "^4.17.21", "moment": "^2.29.1", "moment-timezone": "^0.5.32", "nodemailer": "^6.6.3", "request": "^2.88.0", "request-promise": "^4.2.5" }, "devDependencies": { "@firebase/testing": "^0.20.11", "@types/chai": "^4.2.14", "@types/mocha": "^8.0.3", "@types/node": "^14.14.6", "@types/nodemailer": "^6.4.0", "@types/request": "^2.48.5", "@types/request-promise": "^4.1.46", "chai": "^4.2.0", "firebase-functions-test": "^0.2.3", "fs": "0.0.1-security", "mocha": "^8.2.1", "mocha-typescript": "^1.1.17", "prettier": "^2.1.2", "ts-node": "^9.0.0", "typescript": "^4.3.5" }, "engines": { "node": "10" }, "private": true }
解决方案
1. 确认shared目录的合法性
- 检查
../shared目录下是否存在package.json,且文件中name字段为loanshub-shared,version字段为1.0.0(与tgz文件名匹配) - 若shared是TypeScript项目,需确保已执行编译,且
package.json的main字段指向正确的编译后入口文件(比如lib/index.js)
2. 调整本地依赖的打包与路径
当前npm pack命令将tgz生成在默认位置,Firebase部署时不会自动包含上级目录或外部文件,需修改打包路径:
- 修改
package.json中的pack:loanshub-shared脚本:"pack:loanshub-shared": "npm pack ../shared --pack-destination ./", - 执行
npm run pack:loanshub-shared,确保loanshub-shared-1.0.0.tgz生成在functions目录内 - 重新执行
npm run update-models,确保依赖正确安装
3. 配置Firebase部署前钩子
修改firebase.json,添加predeploy脚本,确保部署前完成依赖打包与安装:
{ "functions": { "predeploy": [ "npm --prefix \"$RESOURCE_DIR\" run build", "npm --prefix \"$RESOURCE_DIR\" run update-models" ] } }
4. 对齐Node版本
当前functions的engines.node为10,与本地Node 12.13.0版本不一致,可能导致依赖安装异常,修改为:
"engines": { "node": "12" }
5. 强制清理缓存部署
执行强制部署命令跳过Firebase CLI缓存:
firebase deploy --only functions --force
同时可删除functions目录下的lib编译产物,重新执行npm run build后再部署。
内容的提问来源于stack exchange,提问作者Khurram Shahzad
相关产品推荐
相关产品推荐

