reactjs Firebase Functions无法从源加载函数定义

h9vpoimq  于 2023-10-17  发布在  React
关注(0)|答案(1)|浏览(106)

我正在处理一个现有的React / Firebase项目,想添加模拟器,讽刺的是,这是为了保存时间。我无法让模拟器在本地运行。
我得到一个相当模糊的错误:
⬢ functions: Failed to load function definition from source: FirebaseError: There was an error reading functions/package.json:
在花了几个小时浏览函数项目中的每一行代码后,我找不到任何问题。当我用途:
firebase deploy --only functions
这也使用了严格的linter,所以即使是未使用的var也无法部署。
最后,我创建了一个全新的项目,使用:
npm create vite@latest my-app --template react
然后我安装了一个新版本的firebase,并使用以下命令完成了安装:
firebase init
当我去运行完全空白的函数项目(firebase emulators:start)时,我得到了完全相同的bug。
我使用的是MacBook Air M2,这可能是问题的一部分吗?我也在运行OpenJDK Runtime Environment来代替Java。
下面是日志:

[debug] [2023-10-12T15:26:17.143Z] ----------------------------------------------------------------------
  [debug] [2023-10-12T15:26:17.143Z] Command:       /Users/user/.nvm/versions/node/v18.16.1/bin/node /Users/user/.nvm/versions/node/v18.16.1/bin/firebase emulators:start
  [debug] [2023-10-12T15:26:17.144Z] CLI Version:   12.7.0
  [debug] [2023-10-12T15:26:17.144Z] Platform:      darwin
  [debug] [2023-10-12T15:26:17.144Z] Node Version:  v18.16.1
  [debug] [2023-10-12T15:26:17.145Z] Time:          Thu Oct 12 2023 16:26:17 GMT+0100 (British Summer Time)
  [debug] [2023-10-12T15:26:17.145Z] ----------------------------------------------------------------------
  [debug] 
  [debug] [2023-10-12T15:26:17.173Z] > 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"]
  [debug] [2023-10-12T15:26:17.173Z] > authorizing via signed-in user ([email protected])
  [debug] [2023-10-12T15:26:17.202Z] openjdk version "21" 2023-09-19

  [debug] [2023-10-12T15:26:17.202Z] OpenJDK Runtime Environment Homebrew (build 21)
  OpenJDK 64-Bit Server VM Homebrew (build 21, mixed mode, sharing)

  [debug] [2023-10-12T15:26:17.205Z] Parsed Java major version: 21
  [info] i  emulators: Starting emulators: auth, functions, firestore, hosting {"metadata":{"emulator":{"name":"hub"},"message":"Starting emulators: auth, functions, firestore, hosting"}}
  [warn] ⚠  hosting: The hosting emulator is configured but there is no hosting configuration. Have you run firebase init hosting? {"metadata":{"emulator":{"name":"hosting"},"message":"The hosting emulator is configured but there is no hosting configuration. Have you run \u001b[1mfirebase init hosting\u001b[22m?"}}
  [debug] [2023-10-12T15:26:17.600Z] [logging] Logging Emulator only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.601Z] [auth] Authentication Emulator only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.601Z] [firestore] Firestore Emulator only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.601Z] [firestore.websocket] websocket server for firestore only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.601Z] assigned listening specs for emulators {"user":{"hub":[{"address":"127.0.0.1","family":"IPv4","port":4400},{"address":"::1","family":"IPv6","port":4400}],"ui":[{"address":"127.0.0.1","family":"IPv4","port":4000},{"address":"::1","family":"IPv6","port":4000}],"logging":[{"address":"127.0.0.1","family":"IPv4","port":4500}],"auth":[{"address":"127.0.0.1","family":"IPv4","port":9099}],"firestore":[{"address":"127.0.0.1","family":"IPv4","port":8080}],"firestore.websocket":[{"address":"127.0.0.1","family":"IPv4","port":9150}]},"metadata":{"message":"assigned listening specs for emulators"}}
  [debug] [2023-10-12T15:26:17.603Z] [hub] writing locator at /var/folders/1s/5h_pyf855sx0w00kjdcnn6cr0000gn/T/hub-project-id.json
  [debug] [2023-10-12T15:26:17.731Z] [functions] Functions Emulator only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.732Z] [eventarc] Eventarc Emulator only supports listening on one address (127.0.0.1). Not listening on ::1
  [debug] [2023-10-12T15:26:17.732Z] late-assigned ports for functions and eventarc emulators {"user":{"hub":[{"address":"127.0.0.1","family":"IPv4","port":4400},{"address":"::1","family":"IPv6","port":4400}],"ui":[{"address":"127.0.0.1","family":"IPv4","port":4000},{"address":"::1","family":"IPv6","port":4000}],"logging":[{"address":"127.0.0.1","family":"IPv4","port":4500}],"auth":[{"address":"127.0.0.1","family":"IPv4","port":9099}],"firestore":[{"address":"127.0.0.1","family":"IPv4","port":8080}],"firestore.websocket":[{"address":"127.0.0.1","family":"IPv4","port":9150}],"functions":[{"address":"127.0.0.1","family":"IPv4","port":5001}],"eventarc":[{"address":"127.0.0.1","family":"IPv4","port":9299}]},"metadata":{"message":"late-assigned ports for functions and eventarc emulators"}}
  [warn] ⚠  functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, hosting, pubsub, storage {"metadata":{"emulator":{"name":"functions"},"message":"The following emulators are not running, calls to these services from the Functions emulator will affect production: \u001b[1mdatabase, hosting, pubsub, storage\u001b[22m"}}
  [debug] [2023-10-12T15:26:17.732Z] defaultcredentials: writing to file /Users/user/.config/firebase/user_company_io_application_default_credentials.json
  [debug] [2023-10-12T15:26:17.733Z] Setting GAC to /Users/user/.config/firebase/user_company_io_application_default_credentials.json {"metadata":{"emulator":{"name":"functions"},"message":"Setting GAC to /Users/user/.config/firebase/user_company_io_application_default_credentials.json"}}
  [debug] [2023-10-12T15:26:17.734Z] >>> [apiv2][query] GET https://firebase.googleapis.com/v1beta1/projects/project-id/adminSdkConfig [none]
  [debug] [2023-10-12T15:26:18.184Z] <<< [apiv2][status] GET https://firebase.googleapis.com/v1beta1/projects/project-id/adminSdkConfig 200
  [debug] [2023-10-12T15:26:18.184Z] <<< [apiv2][body] GET https://firebase.googleapis.com/v1beta1/projects/project-id/adminSdkConfig {"projectId":"project-id","databaseURL":"https://project-id.firebaseio.com","storageBucket":"project-id.appspot.com","locationId":"europe-west"}
  [debug] [2023-10-12T15:26:18.196Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: auto_download"}}
  [debug] [2023-10-12T15:26:18.197Z] Ignoring unsupported arg: single_project_mode_error {"metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: single_project_mode_error"}}
  [debug] [2023-10-12T15:26:18.197Z] Starting Firestore Emulator with command {"binary":"java","args":["-Dgoogle.cloud_firestore.debug_log_level=FINE","-Duser.language=en","-jar","/Users/user/.cache/firebase/emulators/cloud-firestore-emulator-v1.18.2.jar","--host","127.0.0.1","--port",8080,"--websocket_port",9150,"--project_id","project-id","--rules","/Users/user/dev/firebase-test/firestore.rules","--single_project_mode",true,"--functions_emulator","127.0.0.1:5001"],"optionalArgs":["port","webchannel_port","host","rules","websocket_port","functions_emulator","seed_from_export","project_id","single_project_mode"],"joinArgs":false} {"metadata":{"emulator":{"name":"firestore"},"message":"Starting Firestore Emulator with command {\"binary\":\"java\",\"args\":[\"-Dgoogle.cloud_firestore.debug_log_level=FINE\",\"-Duser.language=en\",\"-jar\",\"/Users/user/.cache/firebase/emulators/cloud-firestore-emulator-v1.18.2.jar\",\"--host\",\"127.0.0.1\",\"--port\",8080,\"--websocket_port\",9150,\"--project_id\",\"project-id\",\"--rules\",\"/Users/user/dev/firebase-test/firestore.rules\",\"--single_project_mode\",true,\"--functions_emulator\",\"127.0.0.1:5001\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"websocket_port\",\"functions_emulator\",\"seed_from_export\",\"project_id\",\"single_project_mode\"],\"joinArgs\":false}"}}
  [info] i  firestore: Firestore Emulator logging to firestore-debug.log {"metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"}}
  [debug] [2023-10-12T15:26:18.647Z] Oct 12, 2023 4:26:18 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start
  INFO: Started WebSocket server on ws://127.0.0.1:9150
  {"metadata":{"emulator":{"name":"firestore"},"message":"Oct 12, 2023 4:26:18 PM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start\nINFO: Started WebSocket server on ws://127.0.0.1:9150\n"}}
  [debug] [2023-10-12T15:26:18.660Z] API endpoint: http:// {"metadata":{"emulator":{"name":"firestore"},"message":"API endpoint: http://"}}
  [debug] [2023-10-12T15:26:18.660Z] 127.0.0.1:8080
  If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:

    export FIRESTORE_EMULATOR_HOST=127.0.0.1:8080

  Dev App Server is now running.

  {"metadata":{"emulator":{"name":"firestore"},"message":"127.0.0.1:8080\nIf you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:\n\n   export FIRESTORE_EMULATOR_HOST=127.0.0.1:8080\n\nDev App Server is now running.\n\n"}}
  [info] ✔  firestore: Firestore Emulator UI websocket is running on 9150. {"metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator UI websocket is running on 9150."}}
  [debug] [2023-10-12T15:26:21.247Z] Ignoring unsupported arg: auto_download {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: auto_download"}}
  [debug] [2023-10-12T15:26:21.247Z] Ignoring unsupported arg: port {"metadata":{"emulator":{"name":"ui"},"message":"Ignoring unsupported arg: port"}}
  [debug] [2023-10-12T15:26:21.247Z] Starting Emulator UI with command {"binary":"node","args":["/Users/user/.cache/firebase/emulators/ui-v1.11.7/server/server.js"],"optionalArgs":[],"joinArgs":false} {"metadata":{"emulator":{"name":"ui"},"message":"Starting Emulator UI with command {\"binary\":\"node\",\"args\":[\"/Users/user/.cache/firebase/emulators/ui-v1.11.7/server/server.js\"],\"optionalArgs\":[],\"joinArgs\":false}"}}
  [info] i  ui: Emulator UI logging to ui-debug.log {"metadata":{"emulator":{"name":"ui"},"message":"Emulator UI logging to \u001b[1mui-debug.log\u001b[22m"}}
  [debug] [2023-10-12T15:26:21.297Z] Web / API server started at 127.0.0.1:4000
  {"metadata":{"emulator":{"name":"ui"},"message":"Web / API server started at 127.0.0.1:4000\n"}}
  [debug] [2023-10-12T15:26:21.298Z] Web / API server started at ::1:4000
  {"metadata":{"emulator":{"name":"ui"},"message":"Web / API server started at ::1:4000\n"}}
  [info] i  functions: Watching "/Users/user/dev/firebase-test/functions" for Cloud Functions... {"metadata":{"emulator":{"name":"functions"},"message":"Watching \"/Users/user/dev/firebase-test/functions\" for Cloud Functions..."}}
  [debug] [2023-10-12T15:26:21.502Z] Validating nodejs source
  [debug] [2023-10-12T15:26:22.621Z] > [functions] package.json contents: {
    "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": "18"
    },
    "main": "lib/index.js",
    "dependencies": {
      "firebase-admin": "^11.8.0",
      "firebase-functions": "^4.3.1"
    },
    "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.1.0",
      "typescript": "^4.9.0"
    },
    "private": true
  }
  [error] ⬢  functions: Failed to load function definition from source: FirebaseError: There was an error reading functions/package.json:

  functions/lib/index.js does not exist, can't deploy Cloud Functions {"metadata":{"emulator":{"name":"functions"},"message":"Failed to load function definition from source: FirebaseError: There was an error reading functions/package.json:\n\n functions/lib/index.js does not exist, can't deploy Cloud Functions"}}
  [info] 
  ┌─────────────────────────────────────────────────────────────┐
  │ ✔  All emulators ready! It is now safe to connect your app. │
  │ i  View Emulator UI at http://127.0.0.1:4000/               │
  └─────────────────────────────────────────────────────────────┘

  ┌────────────────┬──────────────────────────────────┬─────────────────────────────────┐
  │ Emulator       │ Host:Port                        │ View in Emulator UI             │
  ├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
  │ Authentication │ 127.0.0.1:9099                   │ http://127.0.0.1:4000/auth      │
  ├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
  │ Functions      │ 127.0.0.1:5001                   │ http://127.0.0.1:4000/functions │
  ├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
  │ Firestore      │ 127.0.0.1:8080                   │ http://127.0.0.1:4000/firestore │
  ├────────────────┼──────────────────────────────────┼─────────────────────────────────┤
  │ Hosting        │ Failed to initialize (see above) │                                 │
  └────────────────┴──────────────────────────────────┴─────────────────────────────────┘
    Emulator Hub running at 127.0.0.1:4400
    Other reserved ports: 4500, 9150

  Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.
z31licg0

z31licg01#

根据您提供的日志,您似乎正在使用TypeScript编写函数。为了能够使用模拟器运行它,您需要首先将其编译为JavaScript。
我能够使用以下步骤成功运行模板:

  • 打开一个终端并运行命令npm create vite@latest my-app
  • 为框架选择React,为变体选择TypeScript
  • 安装Firebase:在my-app目录中运行命令npm install firebase
  • 设置Firebase项目和产品:在my-app目录中运行命令firebase init
  • 选择Firebase项目、产品和模拟器
  • functions目录下,取消注解index.ts文件中的示例helloWorld函数
  • functions目录下,运行命令npm run build来构建应用程序并将代码编译为JavaScript。要进行验证,您应该看到functions/lib目录下的index.js文件
  • 启动模拟器:在my-app目录下运行命令firebase:emulators start --only functions

我希望这能帮上忙。

相关问题