0

I've a very simple typescript firebase function project... the code works and is very simple, but there is something in the project configuration wrong that makes firebase serve NOT recompile the code before starting the server. at same time firebase deploy works perfectly

the project sits as

firebase
-.firebaserc
-firebase.json
-functions
--package.json
--tsconfig.json
--src
---...all the code goes here
--built
---...code after compiled goes here

this is the content of firebase.json

{
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": "npm --prefix functions run build" 
    }
  ]
}

and this is the content of package.json

{
  "name": "functions",
  "scripts": {
    "build": "tsc",
    "build:watch": "tsc --watch",
    "clean-build": "for /D %i in (built\\*) do @rd /s /q \"%i\" & del /q built\\*.*",
    "serve": "npm run clean-build && npm run build && firebase emulators:start --only functions",
    "shell": "npm run clean-build && npm run build && firebase functions:shell",
    "start": "npm run clean-build && npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "20"
  },
  "main": "built/index.js",
  "dependencies": {
    "axios": "^1.6.8",
    "firebase-admin": "^11.8.0",
    "firebase-functions": "^4.9.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^3.1.0",
    "typescript": "^4.9.0"
  },
  "private": true
}

if i am INSIDE functions folder and run npm run serve it works as expected but if i run firebase serve... it only starts the webserver with the current compiled code, any modifications arent reflected and if the code isn't compiled yet it will fail to start

what can i do to fix this?

1 Answer 1

0

It's working as expected. firebase serve does not run any sort of compilation or pre-processing on your code. It just uses what's there (perhaps your previously compiled JavaScript).

The reason why npm run serve works as you expect is because your serve script has an explicit instruction to compile your TS:

"serve": "npm run clean-build && npm run build && firebase emulators:start --only functions",

The npm run build part of the script does that compilation (using tsc) before running the emulator.

If you want a single command to run your compile step before firebase serve, maybe you could create a new script for that. Such as:

"firebase-serve": "npm run clean-build && npm run build && firebase serve",
5
  • it makes firebase serve kind of a very stupid command right? if I do need to create another command in order to make what it should supposed to do... the firebase deploy will compile and deploy the firebase serve should compile, watch and serve that is what is expected from a development time feature... Commented Apr 30, 2024 at 13:55
  • there are a few 6 years old or older requests about this topic on firebase github github.com/firebase/firebase-tools/issues/701 Commented Apr 30, 2024 at 13:57
  • The Firebase CLI just runs JavaScript (and python now for Cloud Functions). It doesn't know how to build your project, and it will likely never know how to do so, because each project could have completely different programming languages and other implementation details. It's up to you to provide those instructions. Commented Apr 30, 2024 at 14:04
  • The firebase deploy command is part of firebase CLI and it knows how to compile my project... it is a little command in a field of the firebase.json file... someone could teach firebase serve to do the same Commented Apr 30, 2024 at 14:10
  • You'll have to address Firebase directly about that. Stack Overflow is a community of volunteers who can't change the direction anything. All we can do is cite facts. Commented Apr 30, 2024 at 14:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.