繁体   English   中英

TypeScript 错误 TS5014:位置 0 处 JSON 中的意外标记 u

[英]TypeScript error TS5014: Unexpected token u in JSON at position 0

我正在尝试将 .ts 编译为 .js

我有tsconfig.json如下

{
"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "outFile": "build/test.js"
},
"exclude": [
    "node_modules"
    ]
}

下面是我的package.json

{
    "name": "test",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "tsc": "^1.20150623.0",
        "typescript": "^2.4.2"
    }
}

自动生成的tasks.json如下所示

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "type": "typescript",
        "tsconfig": "tsconfig.json",
        "problemMatcher": [
            "$tsc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }]
}

当我尝试运行构建任务时,出现以下错误

Executing task: <myprojloc>\node_modules\.bin\tsc.cmd  -p "<myprojloc>\tsconfig.json" <

error TS5014: Failed to parse file '<myprojloc>/tsconfig.json/tsconfig.json': Unexpected token u in JSON at position 0.

Terminal will be reused by tasks, press any key to close it.

我做错了什么? 请注意,我已在package.json中添加了版本

我想说的是,当您忘记将“打字稿”添加为依赖项时,您也会看到此错误。

npm install typescript

应该解决这个问题。

请注意,此依赖项存在于问题的 package.json 中。

如果您仔细查看错误消息,您可以看到失败的原因。 为运行tsc而形成的命令行正在查看错误的目录。 它正在查看<myprojloc>/tsconfig.json/而不是<myprojloc>/ 看看 tsconfig.json 如何在错误中重复两次?

error TS5014: Failed to parse file '<myprojloc>/tsconfig.json/tsconfig.json': Unexpected token u in JSON at position 0.

运行npm install typescript --save-dev对我有用,但我可以看到编辑任务和指定command以在正确的目录中查找 tsconfig.json 也可以解决问题。

保存文件时可能会出现很多错误,从而阻止正确解析。 我通常选择不通过将文件重命名为tsconfig.json.backup或其他东西来处理它,然后调用tsc --init来生成一个已知的好文件。 然后,您可以将您的特定配置转移到新生成的tsconfig.json文件中,取消注释您关心的部分。

如果之后仍然存在,则可能是您使用的 TypeScript 版本中的实际错误。

我在使用Git Bash作为 VS Code 的默认 shell 时遇到了这个问题。 将默认 shell 切换到Command Prompt ,运行npm install typescript以供后代使用,然后再次构建。

我正在使用“默认” tsc 构建任务:

{
    "version": "2.0.0",
    "tasks": [
      {
        "type": "typescript",
        "tsconfig": "tsconfig.json",
        "problemMatcher": ["$tsc"],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
 }

从这个链接尝试了一些东西后,我重新编写了 tasks.json,如下所示,它现在可以工作了。 似乎该命令以前有一些问题

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "taskName": "compile",
        "type": "shell",
        "command": "tsc -p tsconfig.json",
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "problemMatcher": []
    }
]
}

对于面临该错误的每个人,请尝试将整个文件放在一行中。 之后,再次运行您正在尝试的命令并转到错误指向的确切字符。 例如: tsconfig.json: Unexpected token } in JSON at position 1040在那个位置你可能会发现你的问题。 也许是逗号或评论..

就我而言,我有打字稿,但 pbm 是我的tsconfig.json在最后一项,有一些尾随逗号。 删除该文件中的尾随逗号

在此处输入图像描述

一些相关线程: https ://github.com/nrwl/nx/issues/1462#issuecomment-524878788

无需卸载 tsc,只需在项目根级别使用npm install typescript

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM