node.js - Typescript-fel med Firestore-funktioner: SyntaxError: Ov väntat tecken 'export'
Jag gör Fireship Cloud Functions-kursen. I den lektionen täcker han HTTP cloud-funktioner. Jag har bokstavligen rad för rad exakt samma kod men jag får detta fel när jag kör 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.
Jag försökte npm build innan jag körde, jag försökte installera yarn enligt denna rekommendation, och jag har också försökt npm run serve. Ingen har hjälpt.
Jag har node.js v14.18.0 installerad, express.js v4.17.1, firebase v9.4.1, och jag använder Windows 10.
Nedan är hela min https.ts-fil
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!');
});
Nedan är hela min index.ts-fil
export { basicHTTP } from './http';
Och till sist min package.json-fil
{
"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
}
Hur kan jag fixa detta?
Svar #1
Du kan inte distribuera ts-kod, du måste kompilera den till vanlig js. I det här fallet kan du bara använda npm run build och sedan försöka igen.
