簡體   English   中英

Firebase Cloud 函數:Typescript 無法編譯為 JavaScript

[英]Firebase Cloud functions: Typescript does not compile to JavaScript

我計算機上的 Cloud 函數可以使用 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 上嘗試過,它在他的系統上完美運行,但在我的系統上卻沒有。

請幫我解決這個問題。

我遇到了同樣的問題:Typescript 無法編譯。 記錄了很多錯誤,主要是這里的錯誤“錯誤 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 會在我的項目設置中忘記這行特定的代碼。 如果有人知道,請分享。

如果您克隆包含節點項目(包括 Cloud Functions 項目)的存儲庫,您應該做的第一件事是切換到 package.json 定義的目錄,然后運行npm install 這將重建未簽入源代碼管理的 node_modules 的內容。 如果您不運行npm install ,那么該項目實際上將不知道有關 package.json 中定義的任何模塊的任何信息。

我開始知道未安裝 Typescript。

解決了

sudo npm install -g typescript

現在它正在工作 ifne

這為我解決了

在嘗試了許多解決方案以使構建正常工作后,當我取消對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變量已聲明但未使用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM