【问题标题】:Firebase Cloud functions: Typescript does not compile to JavaScriptFirebase Cloud 功能:Typescript 无法编译为 JavaScript
【发布时间】:2020-04-28 20:07:30
【问题描述】:

我计算机上的云功能可以使用 Javascript,但是当我使用 TypeScript 尝试它时,它不会编译为 Javascript。它不会创建 lib/index.js

当我运行 firebase deploy 时,它显示错误

Error: There was an error reading functions/package.json:

firebase deploy --debug 显示以下日志:

APPLEs-MacBook-Air:functions abbasi$ firebase deploy --debug
[2020-01-08T08:39:06.383Z] ----------------------------------------------------------------------
[2020-01-08T08:39:06.390Z] Command:       /usr/local/bin/node /usr/local/bin/firebase deploy --debug
[2020-01-08T08:39:06.390Z] CLI Version:   7.11.0
[2020-01-08T08:39:06.390Z] Platform:      darwin
[2020-01-08T08:39:06.391Z] Node Version:  v12.14.1
[2020-01-08T08:39:06.392Z] Time:          Wed Jan 08 2020 13:39:06 GMT+0500 (Pakistan Standard Time)
[2020-01-08T08:39:06.393Z] ----------------------------------------------------------------------
[2020-01-08T08:39:06.393Z] 
[2020-01-08T08:39:06.422Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[2020-01-08T08:39:06.423Z] > authorizing via signed-in user
[2020-01-08T08:39:06.426Z] [iam] checking project safepay-test for permissions ["cloudfunctions.functions.create","cloudfunctions.functions.delete","cloudfunctions.functions.get","cloudfunctions.functions.list","cloudfunctions.functions.update","cloudfunctions.operations.get","firebase.projects.get"]
[2020-01-08T08:39:06.429Z] >>> HTTP REQUEST POST https://cloudresourcemanager.googleapis.com/v1/projects/safepay-test:testIamPermissions  
 permissions=[cloudfunctions.functions.create, cloudfunctions.functions.delete, cloudfunctions.functions.get, cloudfunctions.functions.list, cloudfunctions.functions.update, cloudfunctions.operations.get, firebase.projects.get]
[2020-01-08T08:39:08.205Z] <<< HTTP RESPONSE 200 content-type=application/json; charset=UTF-8, vary=X-Origin, Referer, Origin,Accept-Encoding, date=Wed, 08 Jan 2020 08:39:08 GMT, server=ESF, cache-control=private, x-xss-protection=0, x-frame-options=SAMEORIGIN, x-content-type-options=nosniff, server-timing=gfet4t7; dur=1374, alt-svc=quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000, accept-ranges=none, transfer-encoding=chunked

=== Deploying to 'safepay-test'...

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
Running command: npm --prefix "$RESOURCE_DIR" run build
✔  functions: Finished running predeploy script.
[2020-01-08T08:39:13.911Z] > [functions] package.json contents: {
  "name": "functions",
  "scripts": {
    "lint": "tslint --project tsconfig.json",
    "build": "tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "8"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^8.6.0",
    "firebase-functions": "^3.3.0"
  },
  "devDependencies": {
    "tslint": "^5.12.0",
    "typescript": "^3.2.2",
    "firebase-functions-test": "^0.1.6"
  },
  "private": true
}

Error: There was an error reading functions/package.json:

 functions/lib/index.js does not exist, can't deploy Cloud Functions

Having trouble? Try firebase [command] --help

我刚刚在另一个同事的 Macbook 上试过,在他的系统上完美运行,但在我的系统上不行。

请帮我解决这个问题。

【问题讨论】:

  • 手动使用tsc 时看到什么错误?输入npx tsc index.js
  • 我应该在终端输入什么?
  • npx tsc lib/index.ts
  • 已解决。

标签: node.js typescript firebase google-cloud-functions


【解决方案1】:

我才知道没有安装 Typescript。

解决

sudo npm install -g typescript

现在可以正常使用了

【讨论】:

  • 你不应该像这样全局安装 typescript。 它只需要安装在你的项目中,在 package.json 中定义,就是这样。也许你从来没有在你的项目中运行npm install 来实际安装它。
【解决方案2】:

如果您克隆包含节点项目(包括 Cloud Functions 项目)的存储库,您应该做的第一件事是更改到定义 package.json 的目录,然后运行 ​​npm install .这将重建未签入源代码控制的 node_modules 的内容。如果你不运行npm install,那么项目实际上不会知道任何关于 package.json 中定义的模块的任何信息。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题: 打字稿只是不会编译。记录了很多错误,主要是这里的错误:"error TS2300: Duplicate identifier 'AbortController'"

    解决办法:

    刚刚在 tsconfig.json 文件的“compilerOptions”属性中添加了以下行:

    "typeRoots": ["node_modules/@types"] 

    这样:

    /functions/tsconfig.json

    
    { 
      {
        "compilerOptions": {
          "module": "commonjs",
          "noImplicitReturns": true,
          "noUnusedLocals": true,
          "outDir": "lib", 
          "sourceMap": true,
          "strict": true,
          "target": "es2017",
          "typeRoots": ["node_modules/@types"]  // <-- here
        },  
        "compileOnSave": true,
        "include": ["src"]
      }
    }
    
    
    

    之后编译就好了!

    ps:不知道为什么如此自动创建所有内容的 firebase cli 会忘记我的项目设置中的这一特定代码行。如果有人知道,请分享。

    【讨论】:

      【解决方案4】:

      这为我解决了问题

      在尝试了许多解决方案以使构建工作后,当我取消注释 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!");
      // });
      

      这是因为导入的functions变量声明了但没有使用

      【讨论】:

        猜你喜欢
        • 2021-03-20
        • 2018-12-03
        • 2020-07-18
        • 2020-04-02
        • 2017-08-15
        • 2019-09-26
        • 2018-12-22
        • 2016-05-25
        相关资源
        最近更新 更多