Firebase功能+ monorepo-部署不工作+ DEPS未在本地安装
看起来某人在这里有同样的问题: https://github.com/firebase/firebase/firebase/firebase - 函数/问题/1050
问题
我已经将项目结构移至具有以下结构的monorepo:
/
|
| - node_modules/
|
| - packages/
| - - app/
| - - - - index.js
| - - - - package.json
| - - functions/
| - - - - src/
| - - - - - - helloWorld.function.js
| - - - - - - index.js
| - - - - package.json
|
| - .firebaserc
| - firebase.json
| - firestore.indexes.json
| - firestore.rules
| - package.json
| - yarn.lock
| - LICENSE.md
据我所知,我们的所有函数所依赖的所有代码都必须在函数
目录中。但是...当我运行纱线安装
时,我function> function
的node_modules
保存在root node_modules文件夹
。
然后,如果我从项目的根部运行firebase部署
,我会得到错误:
错误:解析函数触发器时发生错误。
不存在默认的firebase应用程序。在使用任何firebase服务之前,请确保您致电ditiarizeapp()。
注意:我还没有做任何代码更改...在将项目配置为MonorePo结构之前,一切都很好。
我试图解决此问题的尝试
,我试图尝试使用 nohoist> nohoist 在定义我的工作区时,我的root pockage.json:json:json:json:json:json:json:json:json:json:
{
"private": true,
"name": "@company/project",
"version": "1.0.0",
"description": "Project monorepo",
"repository": "...",
"license": "MIT",
"author": {
"name": "Raul",
"email": "...
},
"scripts": {
"app": "yarn workspace @company/app start",
"documentation": "yarn workspace @company/documentation start",
"server": "yarn workspace @company/server start"
},
"workspaces": {
"packages": ["packages/*"],
"nohoist": ["**/@company/server"] <---- HERE
}
}
- 注意:当我运行纱线服务器
函数shell的启动正确。
,但由于某种原因,在重新安装所有内容之后:
rm -rf node_modules (everywhere)
yarn cache clean
yarn install
函数软件包的依赖项安装在root node_modules
!
中吗?
这就是我的函数node_modules安装deps之后的样子:
node_modules/
.bin/
eslint
eslint-config-prettier
firebase-functions
functions-framework
functions-framework-nodejs
image_size
uuid
qs/
uuid/
这些是所需的依赖项(已安装在root package.json中,这不是我期望的行为):
"name": "@company/server",
...
"dependencies": {
"@google-cloud/functions-framework": "^3.1.1",
"@google-cloud/translate": "^6.3.1",
"@google-cloud/vision": "^2.4.2",
"@googlemaps/google-maps-services-js": "^3.3.3",
"axios": "^0.27.2",
"camelcase": "^6.3.0",
"dotenv": "^16.0.1",
"expo-server-sdk": "^3.6.0",
"firebase-admin": "^10.3.0",
"firebase-functions": "^3.21.2",
"glob": "^7.2.0",
"image-size": "^1.0.1",
"lodash": "^4.17.21",
"moment": "^2.29.3",
"qs": "^6.10.3",
"sharp": "^0.30.6",
"uuid": "^8.3.2"
},
"devDependencies": {
"eslint": "^8.3.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^4.3.1",
"firebase-functions-test": "^0.2.0"
}
更新,
而不是在root中放置,我已放置packages/functions/package.json
中的该细分市场
workspaces: { nohoist: ["**"] }
都在本地安装!
但是...当我做firebase部署
时,相同的错误。如果不是从根文件夹中运行命令,我会从wake> packages/functions/package.json
进行部署成功。
为什么?怎么了?
这是我的firebase.json
文件:
{
"functions": {
"predeploy": ["yarn --cwd \"$RESOURCE_DIR\" lint"],
"source": "packages/functions"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
Looks like someone has the same problem here: https://github.com/firebase/firebase-functions/issues/1050
Problem
I have moved my project structure to a monorepo with the following structure:
/
|
| - node_modules/
|
| - packages/
| - - app/
| - - - - index.js
| - - - - package.json
| - - functions/
| - - - - src/
| - - - - - - helloWorld.function.js
| - - - - - - index.js
| - - - - package.json
|
| - .firebaserc
| - firebase.json
| - firestore.indexes.json
| - firestore.rules
| - package.json
| - yarn.lock
| - LICENSE.md
As far as I know, all code our functions depends on must be inside the functions
directory. But... when I run yarn install
, the node_modules
of my functions
are saved in the root node_modules folder
.
Then, if I run firebase deploy
from the root of my project, I get the error:
Error: Error occurred while parsing your function triggers.
The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.
Note: I haven't made any code change... before configuring the project to the monorepo structure, everything worked fine.
My attempt to solve this problem
I have tried to use nohoist when defining my workspaces inside my root package.json:
{
"private": true,
"name": "@company/project",
"version": "1.0.0",
"description": "Project monorepo",
"repository": "...",
"license": "MIT",
"author": {
"name": "Raul",
"email": "...
},
"scripts": {
"app": "yarn workspace @company/app start",
"documentation": "yarn workspace @company/documentation start",
"server": "yarn workspace @company/server start"
},
"workspaces": {
"packages": ["packages/*"],
"nohoist": ["**/@company/server"] <---- HERE
}
}
-Note: when I run yarn server
the functions shell is started correctly.
But... for some reason, after reinstalling everything:
rm -rf node_modules (everywhere)
yarn cache clean
yarn install
The dependencies of the functions packages are installed in the root node_modules
!
Any ideas?
This is how my functions node_modules looks like after installing the deps:
node_modules/
.bin/
eslint
eslint-config-prettier
firebase-functions
functions-framework
functions-framework-nodejs
image_size
uuid
qs/
uuid/
And these are the dependencies required (which are installed in the root package.json, which is not the behavior I expect):
"name": "@company/server",
...
"dependencies": {
"@google-cloud/functions-framework": "^3.1.1",
"@google-cloud/translate": "^6.3.1",
"@google-cloud/vision": "^2.4.2",
"@googlemaps/google-maps-services-js": "^3.3.3",
"axios": "^0.27.2",
"camelcase": "^6.3.0",
"dotenv": "^16.0.1",
"expo-server-sdk": "^3.6.0",
"firebase-admin": "^10.3.0",
"firebase-functions": "^3.21.2",
"glob": "^7.2.0",
"image-size": "^1.0.1",
"lodash": "^4.17.21",
"moment": "^2.29.3",
"qs": "^6.10.3",
"sharp": "^0.30.6",
"uuid": "^8.3.2"
},
"devDependencies": {
"eslint": "^8.3.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-promise": "^4.3.1",
"firebase-functions-test": "^0.2.0"
}
UPDATE
Instead of nohoisting in the root, I have placed this segment in the packages/functions/package.json
:
workspaces: { nohoist: ["**"] }
and all the deps are installed locally!
But... when I do firebase deploy
, same error. If instead of running the command from the root folder I do it from packages/functions/package.json
the deployment is successful.
Why? What is happening?
This is my firebase.json
file:
{
"functions": {
"predeploy": ["yarn --cwd \"$RESOURCE_DIR\" lint"],
"source": "packages/functions"
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方法(不是正确的解决方案,只是一个技巧):
而不是使用
nohoist
在packages/functions/functions
目录中在本地安装所有dep 目录,而如何将其排除在<<<代码>纱线工作区?为此,将您的root的
package.json
更新为:(packages/functions
不包括作为工作区。)测试:
现在,如果您运行您的项目)
firebase部署
,一切正常...部署成功,我们所有的后端dep均已在本地安装(这是必需的,因为所有云功能代码必须在功能文件夹中)。另外,此 error 也已解决!
优点:
您可以维护单座结构并没有问题部署云功能。
缺点:
这是一个棘手的解决方案...当您从项目的根部运行
YARN install
时,packages/functions/package.json
依赖项未安装(因为我们 工作区中删除它以便在本地安装它们。
从
Workaround (Not the correct solution, just a trick):
Instead of using
nohoist
to installing all the deps locally inside thepackages/functions
directory, what about excluding it from theyarn workspaces
?For this, update your root's
package.json
to:(
packages/functions
is not included as a workspace.)Test it:
Now, if you run (wherever you want in your project)
firebase deploy
, everything works fine... the deployment is successful and all our backend deps are installed locally (it is required, since all the cloud functions code must be inside the functions folder).Also, this error is solved too!
Pros:
You can maintain the monorepository structure and deploy your cloud functions without problems.
Cons:
This is a tricky solution... when you run
yarn install
from the root of your project,packages/functions/package.json
dependencies are not installed (as we have remove it from the workspace in order to install them locally.)In order to solve this little issue, in your root
package.json
, write the following:in my case:
我已经开发了一个解决方案,并写了一篇文章。以下是一个
摘录,但这是 full
文章
I have developed a solution and wrote an article about this. Below is an
excerpt, but here is the full
article