【问题标题】:Typesctipt error with Firestore functions: SyntaxError: Unexpected token 'export'Firestore 函数的 Typescript 错误:SyntaxError: Unexpected token 'export'
【发布时间】:2022-01-15 11:14:25
【问题描述】:

我正在上Fireship Cloud Functions Course 课程。在那节课中,他介绍了 HTTP 云功能。我确实有一行一行的完全相同的代码,但是当我运行firebase serve --only functions时出现此错误@

+  functions: Using node@14 from host.
i  functions: Watching "C:\Users\lover\CS-230\WVU_CS230_2021.08_Group06\COVID19-Tracker\functions" for Cloud Functions...
!  C:\Users\lover\CS-230\WVU_CS230_2021.08_Group06\COVID19-Tracker\functions\src\index.ts:1
export { basicHTTP } from './http';
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at initializeRuntime (C:\Users\lover\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:640:29)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async handleMessage (C:\Users\lover\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:684:20)
!  We were unable to load your functions code. (see above)
   - It appears your code is written in Typescript, which must be compiled before emulation.
   - You may be able to run "npm run build" in your functions directory to resolve this.

我在运行之前尝试过npm build,我尝试按照this recommendation安装yarn,我也尝试过npm run serve。没有一个有帮助。

我安装了 node.js v14.18.0、express.js v4.17.1、firebase v9.4.1,并且我使用的是 Windows 10。

以下是我的 https.ts 文件的全部内容

import * as functions from 'firebase-functions'

import * as admin from 'firebase-admin'
admin.initializeApp(); // we only need to call this once in ONE file! 
                      //if this is done more than once error for duplicate apps will appear 

export const basicHTTP = functions.https.onRequest((request, response) => {
    response.send('Hello from FireBase!');
}); 

下面是我的 index.ts 文件的全部内容

export { basicHTTP } from './http'; 

最后是我的 package.json 文件

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "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": "14"
  },
  "main": "./src/index.ts",
  "dependencies": {
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.14.1"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^3.8.0"
  },
  "private": true
}

任何有关如何解决此问题的帮助都会很棒。提前致谢!

【问题讨论】:

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


    【解决方案1】:

    不能部署ts代码,需要编译成普通js。在这种情况下,您可以只使用 npm run build 然后再试一次。

    【讨论】:

    • 运行 npm run build 后出现同样的错误。我在主目录和函数目录中运行 npm run build 并没有帮助。 export { basicHTTP } from './http'; ^^^^^^ SyntaxError: Unexpected token 'export'
    • 你还在运行index.tx,需要到build文件夹下运行
    • 所以问题出在我的 package.json 文件中。我已经更改了文件,以便 main 包含 ./src/index.ts 当它应该转到 lib/index.js 时,因为就像你说的你不能部署 ts 代码!以前这对我来说没有意义,因为我对此很陌生,但现在确实如此。它现在已经启动并运行。感谢您的帮助@DerEchteKroate!
    猜你喜欢
    • 2018-08-22
    • 1970-01-01
    • 2017-01-30
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多