Jacob Small
Jacob Small

Reputation: 106

Firebase Emulator - "It appears your code is written in Typescript"

I have an angular/fire project and am trying to get cloud functions emulation running. The functions folder is in the root project folder. Inside that folder, I can run npm run build, and I get no errors. Then, I use the command firebase emulators:start --only functions

This returns this error:

⚠  /Users/{{user}}/{{project}}/functions/src/index.ts:1
export {basicHTTP, api} from "./http";
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at initializeRuntime (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:640:29)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
⚠  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.

Yes, this is a typescript project, and I don't know why firebase doesn't recognize that.

Here's my eslintrc.js

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
  ],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx"], // Your TypeScript files extension
      "parserOptions": {
        "project": ["./tsconfig.json"], // Specify it only for TypeScript files
      },
    },
  ],
  "parser": "@typescript-eslint/parser",
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
    "max-len": 0,
    "comma-dangle": 0,
    "require-jsdoc": 0
  },
};

And here is the functions folder's tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src/**/*"
  ]
}

I would love to know why firebase thinks this is not a typescript project and how to fix this error. Any thoughts?

Upvotes: 0

Views: 378

Answers (1)

Jacob Small
Jacob Small

Reputation: 106

I was able to fix this error by changing the reference to main in package.json from "src/index.ts" to "lib/index.js".

Upvotes: 1

Related Questions