[英]Using an Angular library (APF) inside a Firestore cloud function
I created a angular library using angular 14 inside a workspace, added a class with a method, now I'm trying to use it inside the functions folder using typescript, the workspace structure is as follows: I created a angular library using angular 14 inside a workspace, added a class with a method, now I'm trying to use it inside the functions folder using typescript, the workspace structure is as follows:
root
dist
my-lib
...
functions
...
projects
my-lib
my-app
After building the library, I tried using it inside the helloWorld cloud function like this:构建库后,我尝试在 helloWorld 云 function 中使用它,如下所示:
In functions/tsconfig.json under compiler options, added "paths": {"my-lib": ["../dist/my-lib"]}
.在编译器选项下的 functions/tsconfig.json 中,添加了"paths": {"my-lib": ["../dist/my-lib"]}
。
And functions/package.json, added a dependency: "my-lib": "file:../dist/my-lib"
,和functions/package.json,添加了一个依赖: "my-lib": "file:../dist/my-lib"
,
and then in functions/src/index.ts
: import {MyClass} from "my-lib";
然后在functions/src/index.ts
: import {MyClass} from "my-lib";
. .
Problem: when starting the emulator 'firebase emulators:start --only functions' I get this error:问题:启动模拟器“firebase emulators:start --only functions”时出现此错误:
Error: Failed to load function definition from source: Failed to generate manifest from function source: Error [ERR_REQUIRE_ESM]: require() of ES Module C:\angularproject\dist\my-lib\fesm2015\my-lib.mjs not supported.
Instead change the require of C:\angularproject\dist\my-lib\fesm2015\my-lib.mjs to a dynamic import() which is available in all CommonJS modules.
What's the correct way to include a library from an Angular project in a firebase cloud function?在 firebase 云 function 中包含来自 Angular 项目的库的正确方法是什么?
I managed to get this to work by changing the following:我设法通过更改以下内容来使其工作:
functions/tsconfig.json
"module": "es2020",
"target": "es2020",
"moduleResolution": "node",
and in functions/package.json
added并在functions/package.json
添加
"type": "module",
Now I can run the emulator without an issue.现在我可以毫无问题地运行模拟器了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.