简体   繁体   English

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

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

I am trying to compile a .ts to .js我正在尝试将 .ts 编译为 .js

I have tsconfig.json as below我有tsconfig.json如下

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

below is my package.json下面是我的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"
    }
}

and the auto generated tasks.json looks like below自动生成的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
        }
    }]
}

When I try to run the build task, I am getting the below error当我尝试运行构建任务时,出现以下错误

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.

What I am doing wrong?我做错了什么? Please, note I have added the versions in package.json请注意,我已在package.json中添加了版本

I would like to put in that you also see this error when you forgot to add 'typescript' as a dependency.我想说的是,当您忘记将“打字稿”添加为依赖项时,您也会看到此错误。

npm install typescript

should fix that.应该解决这个问题。

Note that this dependency is present in the package.json in the question.请注意,此依赖项存在于问题的 package.json 中。

If you look at the error message carefully, you can see the reason for the failure.如果您仔细查看错误消息,您可以看到失败的原因。 The command line that was formed to run tsc is looking at the wrong directory.为运行tsc而形成的命令行正在查看错误的目录。 It was looking at <myprojloc>/tsconfig.json/ instead of <myprojloc>/ .它正在查看<myprojloc>/tsconfig.json/而不是<myprojloc>/ See how tsconfig.json is repeated twice in the error?看看 tsconfig.json 如何在错误中重复两次?

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

Running npm install typescript --save-dev worked for me, but I can see how editing the task and specifying the command to look in the right directory for tsconfig.json would solve the problem too.运行npm install typescript --save-dev对我有用,但我可以看到编辑任务和指定command以在正确的目录中查找 tsconfig.json 也可以解决问题。

There could be a bunch of things that can go wrong when saving a file that would prevent correct parsing.保存文件时可能会出现很多错误,从而阻止正确解析。 I usually elect to not deal with it by renaming the file to tsconfig.json.backup or something, then invoking tsc --init to generate a known good file.我通常选择不通过将文件重命名为tsconfig.json.backup或其他东西来处理它,然后调用tsc --init来生成一个已知的好文件。 You can then transfer your specific configuration into the newly generated tsconfig.json file, uncommenting the parts you care about.然后,您可以将您的特定配置转移到新生成的tsconfig.json文件中,取消注释您关心的部分。

If it persists after that, it could be an actual bug in the TypeScript version you're on.如果之后仍然存在,则可能是您使用的 TypeScript 版本中的实际错误。

I ran into this issue while using Git Bash as VS Code's default shell.我在使用Git Bash作为 VS Code 的默认 shell 时遇到了这个问题。 Switch the default shell to Command Prompt , run npm install typescript for posterity, and build again.将默认 shell 切换到Command Prompt ,运行npm install typescript以供后代使用,然后再次构建。

I am using the "default" tsc build task:我正在使用“默认” tsc 构建任务:

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

After trying something from this link , I re-write the tasks.json as below and it now worked.从这个链接尝试了一些东西后,我重新编写了 tasks.json,如下所示,它现在可以工作了。 Seems the command has some problem previously似乎该命令以前有一些问题

{
// 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": []
    }
]
}

For everyone that is facing that error, try to put the whole file in one line.对于面临该错误的每个人,请尝试将整个文件放在一行中。 After that, run again the command that you are trying and go to the exact char that the error is pointing.之后,再次运行您正在尝试的命令并转到错误指向的确切字符。 For example: tsconfig.json: Unexpected token } in JSON at position 1040 In that position you will probably find your issue.例如: tsconfig.json: Unexpected token } in JSON at position 1040在那个位置你可能会发现你的问题。 Maybe a comma or a comment..也许是逗号或评论..

For my case, I had typescript but the pbm was my tsconfig.json got some trailling comma , on one last item.就我而言,我有打字稿,但 pbm 是我的tsconfig.json在最后一项,有一些尾随逗号。 Remove trailling commas in that file删除该文件中的尾随逗号

在此处输入图像描述

Some related thread: https://github.com/nrwl/nx/issues/1462#issuecomment-524878788一些相关线程: 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