部署Firebase函数遇Node版本兼容问题,求无需降级解决方案
问题
部署Firebase函数时遇到Node版本不兼容问题:Firebase相关依赖要求Node 18,但本地当前使用Node v20.11.0,同时触发npm报错,具体信息如下:
npm warn EBADENGINE Unsupported engine { npm warn EBADENGINE package: 'backend@1.0.0', npm warn EBADENGINE required: { node: '18' }, npm warn EBADENGINE current: { node: 'v20.11.0', npm: '10.8.1' } npm warn EBADENGINE } npm error code 1 npm error path D:\Projects\Website\server\node_modules\farmhash npm error command failed npm error command C:\WINDOWS\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild npm error prebuild-install warn install Request timed out npm error gyp info it worked if it ends with ok npm error gyp info using node-gyp@10.1.0 npm error gyp info using node@20.11.0 | win32 | x64
项目基于Node 20构建,降级Node版本会影响其他项目依赖,寻求可行解决方案。
解决方案
使用Node版本管理工具隔离版本
安装nvm(Windows)或n(Mac/Linux)这类版本管理工具,为当前Firebase项目单独指定Node 18:- 执行
nvm install 18安装Node 18版本 - 在项目根目录执行
nvm use 18,仅在当前终端会话切换到Node 18,不影响全局和其他项目 - 可在项目根目录创建
.nvmrc文件,写入18,后续进入项目目录执行nvm use即可自动切换版本
- 执行
跳过npm引擎检查并修复farmhash编译问题
- 安装依赖时添加
--ignore-engines参数跳过版本检查:npm install --ignore-engines - 针对farmhash的编译超时问题,先删除
node_modules和package-lock.json,再执行npm install --ignore-engines --verbose - 若仍无法编译,可替换为纯JS实现的替代库
farmhashjs:npm uninstall farmhash && npm install farmhashjs
- 安装依赖时添加
升级Firebase函数运行时到Node 20
目前Firebase Functions已支持Node 20,可调整配置对齐本地版本:- 修改项目根目录的
firebase.json,在functions配置中添加"runtime": "nodejs20" - 更新
package.json中的engines字段为"node": "20" - 重新执行依赖安装和部署命令,避免版本不兼容问题
- 修改项目根目录的
内容的提问来源于stack exchange,提问作者Teekam Singh
相关产品推荐
相关产品推荐

