Search code examples
google-cloud-functionstr

Firebase Cloud Functions TypeScript error: Unknown file extension ".ts" for /workspace/src/index.ts


When I run firebase deploy --only functions I get this error:

Build failed: node:internal/errors:477
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /workspace/src/index.ts
    at new NodeError (node:internal/errors:387:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:76:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:118:38)
    at checkSyntax (node:internal/main/check_syntax:57:26) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}; Error ID: 037e0eb0

Functions deploy had errors with the following functions:
    triggerMe(us-central1)

I'm also getting an error in the Firebase Console showing a red triangle next to this function, saying Function deployment failed. Please try again.

I ran npm outdated and npm update, everything is the latest version.

Here's my index.ts file:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.triggerMe = functions.firestore.document('Triggers/{docId}').onWrite((data, context) => {
    console.log("Trigger warning!")
    console.log(data.message);
    return 30
});

I was getting errors about the data and context parameters being implicit any type so I added "noImplicitAny": false, to tsconfig.json:

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

I also tried switching strict to false. This didn't help.

In my environments.ts file I added useEmulators: true. I get this error whether this property is set to true or false.

Here's my functions/package.json:

{
    "name": "functions",
    "scripts": {
        "build": "tsc",
        "build:watch": "tsc --watch",
        "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": "16"
    },
    "main": "src/index.ts",
    "dependencies": {
        "firebase-admin": "^10.2.0",
        "firebase-functions": "^3.21.0"
    },
    "devDependencies": {
        "typescript": "^4.8.4"
    },
    "private": true
}

And the other package.json file:

{
    "name": "triggerable-functions-tutorial",
    "version": "0.0.0",
    "scripts": {
        "ng": "ng",
        "start": "ng serve",
        "build": "ng build",
        "watch": "ng build --watch --configuration development",
        "test": "ng test"
    },
    "private": true,
    "dependencies": {
        "@angular/animations": "^14.2.0",
        "@angular/common": "^14.2.0",
        "@angular/compiler": "^14.2.0",
        "@angular/core": "^14.2.0",
        "@angular/fire": "^7.4.1",
        "@angular/forms": "^14.2.0",
        "@angular/platform-browser": "^14.2.0",
        "@angular/platform-browser-dynamic": "^14.2.0",
        "@angular/router": "^14.2.0",
        "firebase": "^9.12.1",
        "firebase-admin": "^11.2.0",
        "firebase-functions": "^4.0.1",
        "rxjs": "~7.5.0",
        "tslib": "^2.3.0",
        "zone.js": "~0.11.4"
    },
    "devDependencies": {
        "@angular-devkit/build-angular": "^14.2.6",
        "@angular/cli": "~14.2.6",
        "@angular/compiler-cli": "^14.2.0",
        "@types/jasmine": "~4.3.0",
        "jasmine-core": "~4.4.0",
        "karma": "~6.4.0",
        "karma-chrome-launcher": "~3.1.0",
        "karma-coverage": "~2.2.0",
        "karma-jasmine": "~5.1.0",
        "karma-jasmine-html-reporter": "~2.0.0",
        "typescript": "~4.8.4"
    }
}

And firebase.json:

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": [
    {
      "source": "functions",
      "codebase": "default",
      "ignore": [
        "node_modules",
        ".git",
        "firebase-debug.log",
        "firebase-debug.*.log"
      ],
      "predeploy": [
        "npm --prefix \"$RESOURCE_DIR\" run build"
      ]
    }
  ],
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "ui": {
      "enabled": true
    },
    "singleProjectMode": true
  }
}

Solution

  • Try the following solution:

    1. Remove the "type": "module" from the package.json if added, it looks like you don't have it.
    2. In tsconfig.json, under the compilerOptions, set the "moduleResolution": "Node".

    Like so:

    {
      "compilerOptions": {
        "module": "CommonJS",
        "moduleResolution": "Node"
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "outDir": "lib",
        "sourceMap": true,
        "strict": true,
        "noImplicitAny": false,
        "target": "es2017"
      },
      "compileOnSave": true,
      "include": [
        "src"
      ]
    }
    

    Source: Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts