部署TypeScript应用至Firebase云函数时遇npm依赖缺失错误
解决Firebase云函数部署时npm ci依赖缺失错误
问题描述
部署TypeScript应用到Firebase云函数时构建失败,npm ci过程中提示lock文件缺失多个依赖,错误信息如下:
Build failed: ...sing: idb@7.0.1 from lock file npm ERR! Missing: @firebase/app-check-types@0.4.0 from lock file npm ERR! Missing: selenium-webdriver@4.1.2 from lock file npm ERR! Missing: @firebase/auth-types@0.11.0 from lock file npm ERR! Missing: @firebase/webchannel-wrapper@0.6.2 from lock file npm ERR! Missing: @firebase/firestore-types@2.5.0 from lock file npm ERR! Missing: @firebase/app-check-interop-types@0.1.0 from lock file npm ERR! Missing: @firebase/messaging-interop-types@0.1.0 from lock file npm ERR! Missing: @firebase/functions-types@0.5.0 from lock file npm ERR! Missing: @firebase/installations-types@0.4.0 from lock file npm ERR! Missing: @firebase/performance-types@0.1.0 from lock file npm ERR! Missing: @firebase/remote-config-types@0.2.0 from lock file npm ERR! Missing: @firebase/storage-types@0.6.0 from lock file npm ERR! Missing: @grammyjs/types@2.8.2 from lock file npm ERR! Missing: chokidar@3.5.3 from lock file npm ERR! Missing: debug@3.2.7 from lock file npm ERR! Missing: ignore-by-default@1.0.1 from lock file npm ERR! Missing: pstree.remy@1.1.8 from lock file npm ERR! Missing: semver@5.7.1 from lock file npm ERR! Missing: simple-update-notifier@1.0.7 from lock file npm ERR! Missing: supports-color@5.5.0 from lock file npm ERR! Missing: touch@3.1.0 from lock file npm ERR! Missing: undefsafe@2.0.5 from lock file npm ERR! Missing: anymatch@3.1.2 from lock file npm ERR! Missing: fsevents@2.3.2 from lock file... npm ERR! Clean install a project npm ERR! npm ERR! Usage: npm ERR! npm ci npm ERR! npm ERR! Options: npm ERR! [--no-audit] [--foreground-scripts] [--ignore-scripts] npm ERR! [--script-shell <script-shell>] npm ERR! npm ERR! aliases: clean-install, ic, install-clean, isntall-clean npm ERR! npm ERR! Run "npm help ci" for more info npm ERR! A complete log of this run can be found in: npm ERR! /www-data-home/.npm/_logs/2022-08-22T07_49_29_487Z-debug-0.log; Error ID: beaf8772 Functions deploy had errors with the following functions: router(us-central1) i functions: cleaning up build files...
解决方案
核心原因
npm ci会严格按照package-lock.json的内容安装依赖,一旦lock文件与package.json不一致、lock文件损坏,或者某些依赖仅适配特定平台(如fsevents仅支持macOS),就会触发这类缺失错误。
步骤1:重置本地依赖文件
删除本地的node_modules和package-lock.json,清除旧的依赖记录:
rm -rf node_modules package-lock.json
步骤2:重新生成lock文件
执行本地安装,让npm根据当前package.json生成全新的、一致性的lock文件:
npm install
步骤3:处理平台专属依赖(可选)
如果报错包含fsevents这类平台限定依赖,可将其添加到package.json的optionalDependencies中,避免Linux部署环境报错:
"optionalDependencies": { "fsevents": "^2.3.2" }
添加后重新执行npm install更新lock文件。
步骤4:重新部署云函数
完成上述操作后,重新执行部署命令:
firebase deploy --only functions
额外注意事项
- 禁止手动修改
package-lock.json,所有依赖变更通过npm install/npm uninstall等命令自动同步。 - 尽量保持本地npm版本与Firebase部署环境的npm版本接近,减少lock文件兼容性问题。
内容的提问来源于stack exchange,提问作者Umidbek Ermatov
相关产品推荐
相关产品推荐

