[Fixed] How to share typescript class between firebase cloud functions and angular

Issue

I created an Angular project, inside it I init Firebase Cloud Functions. My folder become like this:

project-root
  node_modules
  src
    app
      (angular components)
  functions
    node_modules
    src
      index.ts

I want some of my typescript classes can be used in both Cloud Functions and Angular, without making copies in both folder, but I don’t know how to do it.

If possible, I also want them to share same node-modules.

Solution

I found a solution that fits my need.

Instead of making a folder above functions and Angular app, just use functions normally and let Angular use files in functions folder.

In Angular’s tsconfig.json

{
  "compilerOptions": {
    "paths": {
      ...
      "@functions/*":["functions/src/*"]
    },
    ...
}

In Angular’s ts file

import { YourClass } from '@functions/your-class.ts'

The final requirement is to let functions use Angular’s npm dependencies.

Go to Angular’s package.json, copy the dependencies you need into functions/package.json.

//functions/package.json

{
  "dependencies": {
    ...
    "rxjs": "~6.4.0"
  },
  ...
}

Now you can use dependencies like rxjs in cloud functions like in Angular without npm install it inside functions.

//functions/src/index.ts

import { Observable, of } from 'rxjs';

Leave a Reply

(*) Required, Your email will not be published