loverde
loverde

Reputation: 81

Typesctipt error with Firestore functions: SyntaxError: Unexpected token 'export'

I am doing the Fireship Cloud Functions Course course. In that lesson he is covering HTTP cloud functions. I have literally line for line the same exact code but I get this error when I run 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.

I tried npm build before running, I tried installing yarn as per this recommendation, and I have also tried npm run serve. None have which have helped.

I have node.js v14.18.0 installed, express.js v4.17.1, firebase v9.4.1, and I am using windows 10.

Below is the entirety of my https.ts file

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!');
}); 

Below is the entirety of my index.ts file

export { basicHTTP } from './http'; 

These are how my directories are organized

And finally my package.json file

{
  "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
}

How can I fix this?

Upvotes: 3

Views: 1137

Answers (1)

Kalle
Kalle

Reputation: 160

You can't deploy ts code, you need to compile it to normal js. In this case you can just use npm run build and then try again.

Upvotes: 2

Related Questions