当我运行
firebase deploy --only functions
时,它会响应有关节点版本不兼容的错误。我的系统安装了节点 20.11.0 和 firebase 13.1.0(使用 asdf
管理)。
在 package.json 中指定
"engines": { "node": ">=18.5" }
,在 firebase.json 中指定 "runtime": "nodejs20"
。
对 Sharp 有依赖,需要节点
^18.17.0 || ^20.3.0 || >=21.0.0
。
firebase deploy --only functions
通过了 lint 和其他步骤,但最终失败了,抱怨找不到 node 18.5.0
(根本不在我的系统上)并且由于尖锐而需要至少 18.7.0
。
我希望整个过程使用节点 20.3.0 运行,但不知道如何实现。
这是实际输出:
✔ functions: Finished running predeploy script.
i functions: preparing codebase typescript 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...
✔ artifactregistry: required API artifactregistry.googleapis.com is enabled
✔ functions: required API cloudfunctions.googleapis.com is enabled
✔ functions: required API cloudbuild.googleapis.com is enabled
i functions: Loading and analyzing source code for codebase typescript to determine what to deploy
Serving at port 8928
Error: Could not load the "sharp" module using the darwin-x64 runtime
Possible solutions:
- Please upgrade Node.js:
Found 18.5.0
Requires ^18.17.0 || ^20.3.0 || >=21.0.0
- Consult the installation documentation:
See https://sharp.pixelplumbing.com/install
at Object.<anonymous> (/Users/troy/Projects/field-tracker-admin-console/functions/node_modules/sharp/lib/sharp.js:114:9)
at Module._compile (node:internal/modules/cjs/loader:1112:14)
at Module._compile (pkg/prelude/bootstrap.js:1890:32)
at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
at Module.load (node:internal/modules/cjs/loader:988:32)
at Module._load (node:internal/modules/cjs/loader:834:12)
at Module.require (node:internal/modules/cjs/loader:1012:19)
at Module.require (pkg/prelude/bootstrap.js:1851:31)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/troy/Projects/field-tracker-admin-console/functions/node_modules/sharp/lib/constructor.js:10:1)
Error: Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
我深入研究了 Nodejs、NPM,并通过 Yarn 进行了一些研究来解决这个问题。
根本问题是我需要特定的节点版本,并且使用全局安装的 firebase 不支持我。我了解到它内置了一个特定的 Nodejs 版本,因此它可以是独立的,并且在大多数情况下不会造成痛苦。但就我而言,我需要最新的 firebase-tools 具有的稍微新的节点版本。看来 firebase-tools 版本 13.1.0 内置了节点 18.5.0,但我的依赖项至少需要 18.7.0。我最终使用了nodejs 20.11.0。
看起来
"runtime": "nodejs20"
中的firebase.json
可以解决这个问题,但事实并非如此。我对情况了解不够,不能说这是一个错误,或者我只是错过了一些东西。
无论如何,我是这样解决的:
在全局安装 firebase-tools 的情况下,
firebase
命令在部署功能时使用其打包的节点版本。
要使用特定版本的节点,请通过
firebase-tools
安装npm
,然后从本地(未打包的)nodejs执行它。
一步一步:
可选择安装
asdf
来管理各个项目中使用的nodejs和其他工具的版本
安装所需版本的
nodejs
- 以下是使用 asdf 的方法:
cd <project root>
asdf add plugin nodejs
asdf install nodejs 20.11.0
asdf local nodejs 20.11.0
在项目的functions目录下,再次使用asdf即可使用同版本的
node
asdf local nodejs 20.11.0
在项目的根目录中,使用
npm
安装 firebase-tools
npm install firebase-tools
更新
firebase.json
和functions/package.json
以引用节点版本
firebase.json
"functions": [
{
"runtime":"nodejs20",
<- snip ->
}]
函数/package.json
"engine":{"node":">=20"},
我是项目的根目录,添加一些方便的脚本到
package.json
以使其在本地nodejs版本中启动本地firebase:
package.json
"scripts": {
"firebase": "firebase",
"df": "firebase deploy --only functions",
< add more here as needed >
}
这些“脚本”的路径中有
node_modules/.bin
,因此,例如,firebase
脚本在本地nodejs中运行node_modules/.bin/firebase
。
我添加了
df
脚本,因为我总是忘记添加双破折号以使 --only 函数起作用。如果没有那些额外的双破折号,该开关将适用于 npm 而不是 firebase。
以下是如何在项目根目录中运行这些脚本:
% npm run df # same as `npm run -- firebase deploy --only functions`
% npm run firebase -V # check the version
我想就是这样 - 所有常用的
firebase
命令现在都可以使用 npm run -- firebase ...
来工作。添加您认为合适的脚本,以便更轻松地正确输入它们。
感谢@DougStevenson 激励我朝正确的方向前进!