简体   繁体   中英

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:

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:

In functions/tsconfig.json under compiler options, added "paths": {"my-lib": ["../dist/my-lib"]} .

And functions/package.json, added a dependency: "my-lib": "file:../dist/my-lib" ,

and then in functions/src/index.ts : import {MyClass} from "my-lib"; .

Problem: when starting the emulator 'firebase emulators:start --only functions' I get this error:

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?

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

"type": "module",

Now I can run the emulator without an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM