Reputation: 770
I am playing with firebase functions. Works fine when deploying to firebase server using command firebase deploy --only functions
. However, I would like to test my functions locally before deploy to server of course. What I see running firebase serve is that the functions "deployed" locally have not the latest changes I did in indext.ts - are running the last builded version, which are in index.js.
My question is, How do I manual build my firebase functions project to test them locally with latest changes?
Should firebase serve
autobuild the project before deploy it locally? For me, it sounds like a bug.
Upvotes: 14
Views: 9962
Reputation: 314
Just use this command when you are using first time npm serve command
firebase emulators:start
Upvotes: -1
Reputation: 126
If you are looking for hot reloading i just did it like below:
"start:emulators": "firebase emulators:start --only functions",
"ts:watch": "tsc --watch",
"dev": "concurrently --kill-others \"npm run start:emulators\" \"npm run ts:watch\"",
It will run both firebase and ts watch in parallel, which will help your development speed
Upvotes: 4
Reputation: 49200
Go to functions folder, open package.json
and add new key: "hotReload": "tsc --watch"
next, run npm run hotReload
in same functions folder and keep the terminal open. This will update *.js
whenever any changes to *.ts
file. Remember that cloud functions run from *.js
file not *.ts
file. Hence, *.ts file needs to compiled to *.js
first.
Next, open new terminal, go to main project folder and run firebase serve
(to parallely run hosting+functions) or firebase serve --only functions
Upvotes: 1
Reputation: 4333
firebase serve
doesn't seem to be running npm build
. But if you look inside functions/package.json
, you can see there is already a serve
script there that performs npm run build && firebase serve --only functions
. So if you just do:
cd functions
npm run serve
You can build and serve without having to do two separate commands.
Upvotes: 8
Reputation: 317412
If you want to reload the changes to your TypeScript when running firebase serve
or firebase functions:shell
, all you have to do is build your project again using npm script that was created for you when you initialized the project (see package.json):
cd functions
npm run build
This will transpile your TypeScript to JavaScript, and the changes to the JavaScript will get picked up by the emulator automatically.
Also, if you are a more advanced TypeScript user, you can run tsc --watch
to automatically compile TS to JS when source files change on disk. You can read more about that in this blog.
Upvotes: 34
Reputation: 3372
I don't think you can just switch between Javascript and Typescript in an initialized project. The setup for Typescript is a bit different than Javascript. You will need to migrate your Javascript project to Typescript.
To migrate your project from JS to TS follow this firebase-functions documentation on :
Migrating an existing JavaScript Cloud Functions project
Typescript project setup:
Upvotes: 1