在index.mts中运行npm start时出现Typescript错误

rggaifut  于 2023-06-07  发布在  TypeScript
关注(0)|答案(1)|浏览(387)

无论我做什么,我总是在我的index.mts文件中得到一些类型的错误。

node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".mts" for /Users/CMO/Desktop/ts-course/l09-final/index.mts
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:139:38)
    at defaultLoad (node:internal/modules/esm/load:83:20)
    at DefaultModuleLoader.load (node:internal/modules/esm/loader:319:26)
    at DefaultModuleLoader.moduleProvider (node:internal/modules/esm/loader:194:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at #createModuleJob (node:internal/modules/esm/loader:218:17)
    at DefaultModuleLoader.getJobFromResolveResult (node:internal/modules/esm/loader:171:34)
    at DefaultModuleLoader.getModuleJob (node:internal/modules/esm/loader:156:17) {   code: 'ERR_UNKNOWN_FILE_EXTENSION' }

Node.js v20.0.0
这里是我的package.json

{
    "name": "final-project",
    "version": "1.0.0",
    "description": "Testing TypeScript code",
    "main": "index.mts",
    "type": "module",
    "scripts": {
        "start": "tsc && node index.mts"
    },
    "dependencies": {
        "moment": "^2.29.4",
        "randomstring": "^1.2.3"
    },
    "devDependencies": {
        "@types/randomstring": "^1.1.8",
        "typescript": "^5.0.4"
    }
}
jchrr9hc

jchrr9hc1#

检查您的节点版本。如果您已遵循此链接上建议的所有内容:Can't run my Node.js Typescript project TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /app/src/App.ts,然后我会尝试降级您的节点版本。
我在20.2.0上运行,并尝试了那里建议的一切。一位同事在他们运行19.8.1版本的机器上试用了它,它工作正常。在我降级后,我能够通过这一步。

相关问题