简体   繁体   English

本地测试 firebase 功能与 typescript

[英]local test firebase functions with typescript

I have a problem with local test functions in typescript. Now, I used npm run build && firebase serve --only functions to run local functions, but every change I made on the code, I need to run this command again to see the result.我在 typescript 中的本地测试功能有问题。现在,我使用npm run build && firebase serve --only functions来运行本地功能,但是我对代码所做的每一个更改,我都需要再次运行此命令才能看到结果. I want to view real-time change without having to build the typescript code every time.我想查看实时变化,而不必每次都构建 typescript 代码。 Is there a solution to this problem?这个问题有解决办法吗?

The Firebase emulator will pick up changes to JavaScript automatically while it's running. Firebase 仿真器将在运行时自动获取对 JavaScript 的更改。 Since you're using TypeScript, you will have to compile to JavaScript in order for the emulator to pick up the change.由于您使用的是 TypeScript,因此您必须编译为 JavaScript 才能让模拟器接受更改。 You have two main choices:您有两个主要选择:

  • Run npm run build again after each change每次更改后再次运行npm run build
  • Tell the TypeScript compile to "watch" your source files and compile them whenever there is a change.告诉 TypeScript 编译以“监视”您的源文件并在发生更改时编译它们。 You can do that with npx tsc --watch .你可以用npx tsc --watch做到这一点。

This is how I run both command together for my typescript functions这就是我为我的 typescript 函数同时运行两个命令的方式

tsc --watch & firebase serve --only functions

That is my package.json and an npm scripts so I can invoke it easily with one command and have it update whenever I make changes.那是我的 package.json 和 npm 脚本,所以我可以用一个命令轻松调用它,并在我进行更改时更新它。

This Solved it这解决了

Stop using firebase serve and use npm run serve after you cd to your functions folder.停止使用firebase serve并使用npm run serve在您 cd 到您的函数文件夹后。

npm run serve

This will automatically update changes on your typescript before serving这将在服务之前自动更新您的 typescript 上的更改

run tsc --watch in the same time you run the emulator to transpile ts build files to js on every change.在运行模拟器的同时运行tsc --watch以在每次更改时将 ts 构建文件转换为 js。

replace:代替:

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

with:和:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 firebase 云功能中具有 typescript 的 firebase-admin - firebase-admin with typescript in firebase cloud functions 如何在typescript函数中获取Firebase中的TImestamp类型? - How to get TImestamp type in Firebase functions in typescript? React + Firebase Cloud Functions in Typescript部署失败 - React + Firebase Cloud Functions in Typescript fails to deploy Next.js 上 Firebase 功能与 Typescript - Next.js on Firebase Functions with Typescript TypeScript 不识别导出 Firebase 云函数 - TypeScript Not Recognising Exported Firebase Cloud Functions Firebase 函数保存 JSON 到本地文件 - Firebase functions saving JSON to local file 如何将 Firebase 参数化配置与 Typescript 一起用于 Cloud Functions? - How can I use Firebase Parameterized configuration with Typescript for Cloud Functions? 为什么我的 Firebase Functions 模拟器没有运行我在 Typescript 中所做的更改? - Why is my Firebase Functions emulator not running my changes in Typescript? 通过 Firebase 功能将图片下载到用户本地机器 - Download images to users local machines via Firebase Functions 我正在尝试将我的简单 typescript 应用程序部署到 firebase 云功能。 但我有这个错误 - I'm trying to deploy my simple typescript app to firebase cloud functions. But I have got this error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM