初始化 gts 后部署 firebase 函数时出错

问题描述 投票:0回答:1

意向:

我想在我的 Firebase Functions 项目中使用 Google 的 TypeScript 样式指南 gts

预期结果:

命令

firebase deploy --only functions
运行
npx gts init
后仍应成功部署我的功能,不会出现错误。

实际结果:

即使修复了许多明显的问题,我的功能部署仍然失败。

重现步骤:

  1. 通过在空文件夹中运行
    firebase init functions
    来初始化 firebase 函数项目。
  2. src/
    目录下添加相关功能代码。
  3. 运行
    "deploy"
    脚本,或从终端运行
    firebase deploy --only functions
    • 结果:功能部署没有任何问题。
  4. npx gts init
    目录下运行
    functions/
    来初始化gts。
  5. 运行
    "deploy"
    脚本,或从终端运行
    firebase deploy --only functions
    • 结果:部署失败:
      • 错误:读取functions\package.json时出错:
        functions\lib\index.js 不存在,无法部署 Cloud Functions
  6. 修复目录不匹配的问题。
    • 结果:部署仍然失败,并出现相同的错误。
  7. 修复
    "main"
    的位置。
    • 结果:部署失败:
      • 错误:npm.cmd:未找到。
  8. 通过从脚本中删除
    npm.cmd: not found
    来修复
    .cmd
    • 结果:部署失败:
      • 错误:
        tsc: not found

下面我列出了上面列表中一些关键阶段的项目结构和相关项目文件的内容。
如果看起来很冗长,我很抱歉,但我认为最好尝试涵盖大部分潜在的相关信息。

初始化 Firebase 函数项目之后,初始化 gts 之前:

项目具有标准的 Firebase Functions 结构。

my-project
 +- functions/     # Directory containing all your functions code
      |
      +- lib/
      |   |
      |   +- index.js  # Built/transpiled JavaScript code
      |   |
      |   +- index.js.map # Source map for debugging
      |
      +- src/     # Directory containing TypeScript source
      |   |
      |   +- index.ts  # main source file for your Cloud Functions code
      |
      +- .eslintrc.js # Optional file if you enabled ESLint
      |
      +- package.json  # npm package file describing your Cloud Functions code
      |
      +- tsconfig.dev.json # Optional file that references .eslintrc.js
      |
      +- tsconfig.json

.eslintrc.js

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  },
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
    "indent": ["error", 2],
  },
};

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^3.0.0",
    "typescript": "^4.9.0"
  },
  "private": true
}

tsconfig.dev.json

{
  "include": [
    ".eslintrc.js"
  ]
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

在上述项目状态中,

"deploy"
脚本成功完成。功能部署没有任何错误。

初始化gts后:

项目结构。

my-project
 +- functions/     # Directory containing all your functions code
      |
      +- lib/
      |   |
      |   +- index.js  # Built/transpiled JavaScript code
      |   |
      |   +- index.js.map # Source map for debugging
      |
      +- src/     # Directory containing TypeScript source
      |   |
      |   +- index.ts  # main source file for your Cloud Functions code
      |
      +- .editorconfig
      |
      +- .eslintignore
      |
      +- .eslintrc.js # Optional file if you enabled ESLint
      |
      +- .eslintrc.json
      |
      +- .prettierrc.js
      |
      +- package.json  # npm package file describing your Cloud Functions code
      |
      +- tsconfig.dev.json # Optional file that references .eslintrc.js
      |
      +- tsconfig.json

.editorconfig

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
insert_final_newline = true

.eslintignore

build/

.eslintrc.js

没有变化。

.eslintrc.json

{
  "extends": "./node_modules/gts/"
}

.prettierrc.js

module.exports = {
  ...require('gts/.prettierrc.json')
}

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "gts lint",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "clean": "gts clean",
    "compile": "tsc",
    "fix": "gts fix",
    "prepare": "npm.cmd run compile",
    "pretest": "npm.cmd run compile",
    "posttest": "npm.cmd run lint"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^3.0.0",
    "typescript": "~4.7.0",
    "gts": "^3.1.1",
    "@types/node": "^14.11.2"
  },
  "private": true
}

tsconfig.dev.json

没有变化。

tsconfig.json

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "build"
  },
  "include": [
    "src/**/*.ts",
    "test/**/*.ts"
  ]
}

在上述项目状态中,

"deploy"
脚本失败并出现错误:

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

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

此错误是因为默认情况下 Firebase 函数将

"outDir"
中的
tsconfig.json
设置为
"lib"
,但初始化 gts 会将
"outDir"
更改为
"build"

我不确定如何更改
"deploy"
脚本以期望
"outDir"
"build"
,因此我将
"outDir"
恢复为
"lib"

恢复到
"outDir": "lib"
中的
tsconfig.json
后:

tsconfig.json

{
  "extends": "./node_modules/gts/tsconfig-google.json",
  "compilerOptions": {
    "rootDir": ".",
    "outDir": "lib"
  },
  "include": [
    "src/**/*.ts",
    "test/**/*.ts"
  ]
}

在此项目状态中,

"deploy"
脚本再次失败并出现错误:

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

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

这次错误是因为

index.js
被构建到目录
functions\lib\src\
而不是
functions\lib\

我通过将
"main": "lib/index.js"
中的
package.json
更改为
"main": "lib/src/index.js"
解决了这个问题。

"main": "lib/src/index.js"
中设置
package.json
后:

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "gts lint",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "clean": "gts clean",
    "compile": "tsc",
    "fix": "gts fix",
    "prepare": "npm.cmd run compile",
    "pretest": "npm.cmd run compile",
    "posttest": "npm.cmd run lint"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/src/index.js",
  "dependencies": {
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^3.0.0",
    "typescript": "~4.7.0",
    "gts": "^3.1.1",
    "@types/node": "^14.11.2"
  },
  "private": true
}

在此项目状态下,

"deploy"
脚本失败,如下所示:

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

> lint
> gts lint

version: 16

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

> build
> tsc

+  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudbuild.googleapis.com is enabled
+  functions: required API cloudfunctions.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged D:\Projects\my-project\functions (127.84 KB) for uploading
i  functions: ensuring required API identitytoolkit.googleapis.com is enabled...
+  functions: required API identitytoolkit.googleapis.com is enabled
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function userBeforeCreate(us-central1)...
i  functions: updating Node.js 16 function userOnCreate(us-central1)...
Build failed: > prepare
> npm.cmd run compile

sh: 1: npm.cmd: not found
npm ERR! code 127
npm ERR! path /workspace
npm ERR! command failed
npm ERR! command sh -c -- npm.cmd run compile

npm ERR! A complete log of this run can be found in:
npm ERR!     /www-data-home/.npm/_logs/2023-05-05T09_23_49_048Z-debug-0.log; Error ID: beaf8772
Build failed: > prepare
> npm.cmd run compile

sh: 1: npm.cmd: not found
npm ERR! code 127
npm ERR! path /workspace
npm ERR! command failed
npm ERR! command sh -c -- npm.cmd run compile

npm ERR! A complete log of this run can be found in:
npm ERR!     /www-data-home/.npm/_logs/2023-05-05T09_24_40_038Z-debug-0.log; Error ID: beaf8772

Functions deploy had errors with the following functions:
        userBeforeCreate(us-central1)
        userOnCreate(us-central1)
i  functions: cleaning up build files...
Error: There was an error deploying functions:
- Error Failed to update function userBeforeCreate in region us-central1
- Error Failed to update function userOnCreate in region us-central1

Process finished with exit code 2

这是提到的 Google Cloud 日志之一(它们本质上是相同的)[注意:ids 等已被 xxxxx 隐藏]:

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 3,
      "message": "Build failed: > prepare\n> npm.cmd run compile\n\nsh: 1: npm.cmd: not found\nnpm ERR! code 127\nnpm ERR! path /workspace\nnpm ERR! command failed\nnpm ERR! command sh -c -- npm.cmd run compile\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /www-data-home/.npm/_logs/2023-05-05T09_10_41_708Z-debug-0.log; Error ID: beaf8772"
    },
    "authenticationInfo": {},
    "serviceName": "cloudfunctions.googleapis.com",
    "methodName": "google.cloud.functions.v1.CloudFunctionsService.UpdateFunction",
    "resourceName": "projects/my-project/locations/us-central1/functions/userBeforeCreate"
  },
  "insertId": "xxxxx",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "region": "us-central1",
      "function_name": "userBeforeCreate",
      "project_id": "my-project"
    }
  },
  "timestamp": "2023-05-05T09:10:59.793425Z",
  "severity": "ERROR",
  "logName": "projects/my-project/logs/cloudaudit.googleapis.com%2Factivity",
  "operation": {
    "id": "operations/xxxxx",
    "producer": "cloudfunctions.googleapis.com",
    "last": true
  },
  "receiveTimestamp": "2023-05-05T09:11:00.085118556Z"
}

为了解决

npm.cmd: not found
问题,我尝试从
.cmd
中的
"prepare"
"pretest"
"posttest"
脚本中删除
package.json

.cmd
"prepare"
"pretest"
脚本中删除
"posttest"
后:

package.json

{
  "name": "functions",
  "scripts": {
    "lint": "gts lint",
    "build": "tsc",
    "build:watch": "tsc --watch",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log",
    "clean": "gts clean",
    "compile": "tsc",
    "fix": "gts fix",
    "prepare": "npm run compile",
    "pretest": "npm run compile",
    "posttest": "npm run lint"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/src/index.js",
  "dependencies": {
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^5.12.0",
    "@typescript-eslint/parser": "^5.12.0",
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^3.0.0",
    "typescript": "~4.7.0",
    "gts": "^3.1.1",
    "@types/node": "^14.11.2"
  },
  "private": true
}

在此项目状态下,

"deploy"
脚本失败,如下所示:

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

> lint
> gts lint

version: 16

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

> build
> tsc

+  functions: Finished running predeploy script.
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged D:\Projects\my-project\functions (127.96 KB) for uploading
i  functions: ensuring required API identitytoolkit.googleapis.com is enabled...
+  functions: required API identitytoolkit.googleapis.com is enabled
+  functions: functions folder uploaded successfully
i  functions: updating Node.js 16 function userBeforeCreate(us-central1)...
i  functions: updating Node.js 16 function userOnCreate(us-central1)...
Build failed: > prepare
> npm run compile


> compile
> tsc

sh: 1: tsc: not found
npm ERR! code 127
npm ERR! path /workspace
npm ERR! command failed
npm ERR! command sh -c -- npm run compile

npm ERR! A complete log of this run can be found in:
npm ERR!     /www-data-home/.npm/_logs/2023-05-05T10_46_49_310Z-debug-0.log; Error ID: beaf8772
Build failed: > prepare
> npm run compile


> compile
> tsc

sh: 1: tsc: not found
npm ERR! code 127
npm ERR! path /workspace
npm ERR! command failed
npm ERR! command sh -c -- npm run compile

npm ERR! A complete log of this run can be found in:
npm ERR!     /www-data-home/.npm/_logs/2023-05-05T10_47_18_266Z-debug-0.log; Error ID: beaf8772

Functions deploy had errors with the following functions:
        userBeforeCreate(us-central1)
        userOnCreate(us-central1)
i  functions: cleaning up build files...
Error: There was an error deploying functions:
- Error Failed to update function userBeforeCreate in region us-central1
- Error Failed to update function userOnCreate in region us-central1

Process finished with exit code 2

我不知道如何从这里解决问题,因此我们将不胜感激。

typescript firebase google-cloud-functions
1个回答
0
投票

我已成功重现您的问题。 以下是我所做的步骤:

  1. 使用您提供的代码在名为
    "FirebaseGTS"
    的透明文件夹中创建了一个项目
    "firebase init functions"
  2. 填写所需的必要信息。例如(现有项目、项目 ID)

然后

语言:

TypeScript

您想使用 ESLint 来捕获可能的错误并强制执行样式吗? :

"NO"

它将自动创建以下内容:

Wrote functions/package.json
Wrote functions/tsconfig.json
Wrote functions/src/index.ts
Wrote functions/.gitignore

下一个

现在使用 npm 安装依赖项吗? :

YES
这会添加所需的所有软件包。

  1. 我用过
    firebase deploy --only functions

结果:部署没有任何问题。

  1. 现在我在函数文件夹中安装了
    npx gts init
    ,正如你提到的。

Package.json 不存在。产生 ? :

YES

“自动生成”

Writes package.json
Writes ./tsconfig.json
Writes ./.eslintrc.json
Writes ./.eslintignore
Writes ./.prettierrc.js
Writes ./.editorconfig

结果:添加了包,并且需要审核包。

注意:不要覆盖 devDependency,因为您拥有最新的 v4.9.0 而不是 v4.7.0

  1. 在函数文件夹内,我刚刚做了
    firebase deploy --only functions

结果:部署完成!

© www.soinside.com 2019 - 2024. All rights reserved.