首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Firebase函数TypeScript错误:/工作区/src/index.ts的未知文件扩展名".ts“

Firebase函数TypeScript错误:/工作区/src/index.ts的未知文件扩展名".ts“
EN

Stack Overflow用户
提问于 2022-10-20 01:39:16
回答 2查看 107关注 0票数 1

当我运行firebase deploy --only functions时,会得到以下错误:

代码语言:javascript
运行
复制
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)

我还在Firebase控制台中得到一个错误,该函数旁边显示一个红色三角形,表示Function deployment failed. Please try again.

我运行了npm outdatednpm update,一切都是最新版本。

这是我的index.ts文件:

代码语言:javascript
运行
复制
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
});

我收到关于datacontext参数是隐式any类型的错误,所以我将"noImplicitAny": false,添加到tsconfig.json

代码语言:javascript
运行
复制
{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": false,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

我还尝试将strict转换为false。这没什么用。

在我的environments.ts文件中添加了useEmulators: true。无论该属性设置为true还是false,我都会得到这个错误。

这是我的functions/package.json

代码语言:javascript
运行
复制
{
    "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
}

另一个package.json文件:

代码语言:javascript
运行
复制
{
    "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"
    }
}

firebase.json

代码语言:javascript
运行
复制
{
  "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
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-20 01:55:55

尝试以下解决方案:

如果添加了"moduleResolution": "Node".,则

  1. package.json中删除"type": "module",看起来您没有它。

就像这样:

代码语言:javascript
运行
复制
{
  "compilerOptions": {
    "module": "CommonJS",
    "moduleResolution": "Node"
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": false,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

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

票数 1
EN

Stack Overflow用户

发布于 2022-10-20 16:39:36

我将"type": "module",添加到package.json中。这消除了ERR_UNKNOWN_FILE_EXTENSION错误。我还将依赖项升级到最新版本。

代码语言:javascript
运行
复制
{
    "name": "functions",
    "type": "module",
    "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": "^11.2.0",
        "firebase-functions": "^4.0.1"
    },
    "devDependencies": {
        "typescript": "^4.8.4"
    },
    "private": true
}

我现在得到了这个错误:

代码语言:javascript
运行
复制
Function failed on loading user code. This is likely due to a bug in the user code. 

我将(data, context)更改为(data: any, context: any),该错误被替换为此错误:

代码语言:javascript
运行
复制
SyntaxError: Unexpected token ':'

我有"noImplicitAny": false,tsconfig.json。将其更改为true会引发预期的错误(指示该属性正在工作):

代码语言:javascript
运行
复制
Parameter 'data' implicitly has an 'any' type.

我删除了noImplicitAny,并将strict设置为false。这引发了以下错误:

代码语言:javascript
运行
复制
Function failed on loading user code. This is likely due to a bug in the user code.

我放弃并使用JavaScript。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74133574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档