能否在单个Firebase项目中混用Python与Node.js编写的Google Cloud Functions?
在同一Firebase项目中同时用Python和Node.js写Cloud Functions
当然可以在同一个Firebase项目里同时部署Python和Node.js编写的Cloud Functions,核心是通过firebase.json给不同函数指定对应的源码目录和运行时,同时把两种语言的代码分开存放就行。
1. 整理项目目录结构
把两种语言的函数代码分开,比如在项目根目录下建两个子目录(比如functions-nodejs和functions-python),各自放对应语言的代码和依赖文件:
你的Firebase项目/ ├── functions-nodejs/ │ ├── index.js │ ├── package.json │ └── node_modules/ ├── functions-python/ │ ├── main.py │ ├── requirements.txt │ └── venv/ └── firebase.json
2. 配置firebase.json
在firebase.json里,给每组同语言的函数配置source(源码目录)和runtime(运行时版本),示例如下:
{ "functions": [ { "source": "functions-nodejs", "runtime": "nodejs18", "ignore": [ "node_modules", ".git", "firebase-debug.log", "firebase-debug.*.log" ] }, { "source": "functions-python", "runtime": "python311", "ignore": [ "venv", ".git", "firebase-debug.log", "firebase-debug.*.log" ] } ] }
- 每个对象对应一组同语言的函数,
source指向该语言的代码目录,runtime选你用的Node.js/Python版本(注意要选Firebase支持的版本)。 ignore用来排除不需要部署的文件,比如依赖包、虚拟环境这些。
3. 部署函数
- 部署所有函数:直接跑
firebase deploy --only functions,Firebase会自动识别不同目录的函数,按配置的运行时部署。 - 只部署某一种语言的函数:比如只部署Node.js的,跑
firebase deploy --only functions:functions-nodejs;只部署Python的就跑firebase deploy --only functions:functions-python。 - 部署单个函数:如果某个语言下有多个函数,也可以指定单个部署,比如
firebase deploy --only functions:你的Node.js函数名或者firebase deploy --only functions:你的Python函数名。
要注意的点
- 每个语言的目录里得有正确的依赖配置:Node.js目录要有
package.json,Python目录要有requirements.txt。 - 运行时版本要选Firebase支持的,别用已经废弃的版本。
- 两种语言的函数名字不能重复,不然部署会冲突。
内容的提问来源于stack exchange,提问作者Nico Claes
相关产品推荐
相关产品推荐

