首页 > 解决方案 > Typescript 中的 React + Firebase 云功能无法部署

问题描述

我是我的项目,我想使用

部署时遇到错误。很容易重现(现在跳过 firestore 和 firebase 托管初始化)

yarn create react-app sample-app
cd sample-app
firebase init
(select functions by marking a box interactively)
(select typescript by marking a box interactively)
(uncomment in functions/src/index.ts)

函数/src/index.ts 是

import * as functions from 'firebase-functions';

// Start writing Firebase Functions
// https://firebase.google.com/docs/functions/typescript
export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

然后部署失败

firebase deploy

=== Deploying to 'myproject'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/username/sample-app/functions
> tslint --project tsconfig.json

Running command: npm --prefix "$RESOURCE_DIR" run build

> functions@ build /Users/username/sample-app/functions
> tsc

../node_modules/@types/testing-library__react/index.d.ts:13:49 - error TS7016: Could not find a declaration file for module '@testing-library/dom'. '/Users/username/sample-app/node_modules/@testing-library/dom/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/testing-library__dom` if it exists or add a new declaration (.d.ts) file containing `declare module '@testing-library/dom';`

13 import { queries, Queries, BoundFunction } from '@testing-library/dom';
                                                   ~~~~~~~~~~~~~~~~~~~~~~

../node_modules/@types/testing-library__react/index.d.ts:16:15 - error TS7016: Could not find a declaration file for module '@testing-library/dom'. '/Users/username/sample-app/node_modules/@testing-library/dom/dist/index.js' implicitly has an 'any' type.
  Try `npm install @types/testing-library__dom` if it exists or add a new declaration (.d.ts) file containing `declare module '@testing-library/dom';`

16 export * from '@testing-library/dom';
                 ~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/username/.npm/_logs/2020-08-04T06_14_06_195Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

即使我 npm install @types/testing-library__dom 在 root 和 functions/ 中做了,部署也失败了。

并且根目录有

ls
README.md     firebase.json functions     node_modules  package.json  public        src           tsconfig.json yarn.lock

欢迎任何想法。

标签: reactjstypescriptfirebasegoogle-cloud-functions

解决方案


你可以试试这个解决方案是否适合你?

https://stackoverflow.com/a/49309428/1673761

将此行添加到函数文件夹中的 tsconfig:

{                                                                                       
  "compilerOptions": { 
    ...
    "typeRoots": [
      "node_modules/@types",
    ],
    ...
}

这是为我工作的“compilerOptions”块的一部分


推荐阅读