Serving Firebase Functions doesn't hot reload on Windows

10.5k Views Asked by At

Serving Firebase Functions locally with the firebase serve command is supposed to enable hot reloading but this doesn't seem to work on windows, even with watchman installed. Is there a better solution aside from running npm build after each code change?

5

There are 5 best solutions below

0
On

Try following npm script

"serve": "./node_modules/.bin/tsc --watch | firebase serve --only functions"
0
On

It works as of the current point in time (firebase-tools 8.0.0).

Both

firebase serve --only functions

and

 firebase emulators:start --only functions

work but one still needs to run npm run build manually each time.

0
On

I do it the following way (additionally to galki`s answer):

firebase serve --only functions

and in another terminal tab I just use

npm run build -- --watch 

from within functions directory. This way you have some kind of faster dev cycle.

0
On

To enable hot reloading on firebase functions with typescript you can add this 2 commands to your package.json file

"build:watch": "tsc -w"
"serve:watch": "npm run build:watch | firebase emulators:start --only functions",

If you want also to use path aliases feature you will be required to install 2 additional dev packages in order to make it work

npm install --save-dev tsc-alias concurrently

tsc-alias is for replacing alias paths with relative paths after typescript compilation of tsc compiler because the compiler alone can't resolve the alias paths

concurrently is for runing multiple commands concurrently

After installing the 2 packages you will need to add this 2 scripts to your package.json file

"build:watch": "concurrently --kill-others \"tsc -w\" \"tsc-alias -w\"",
"serve:watch": "npm run build:watch | firebase emulators:start --only functions",

Starting of development with hot reload then will be as easy as running only in the terminal

npm run serve:watch

Please Note: I am using this versions of the packages

"firebase-admin": "^10.0.1",
"firebase-functions": "^3.14.1",
"tsc-alias": "^1.5.0",
"typescript": "^4.5.5",
"concurrently": "^7.0.0",

Older or newer versions might introduce some issues with compiling the code

1
On

I mixed the 2 answers:

"scripts": {
   "serve": "npm run build -- --watch | firebase emulators:start --only functions",
   ...
}