【发布时间】: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