【问题标题】:TypeScript error TS5014: Unexpected token u in JSON at position 0TypeScript 错误 TS5014:位置 0 处 JSON 中的意外标记 u
【发布时间】:2018-01-29 02:52:16
【问题描述】:

我正在尝试将 .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中添加了版本

【问题讨论】:

  • 也许您在文件开头有 BOM [en.wikipedia.org/wiki/Byte_order_mark]
  • 我也保存了没有 BOM 的文件。还是一样的@Felix
  • 还在 tsconfig.json 中添加了 "emitBOM": false。没有区别

标签: javascript json typescript


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      在尝试了 link 之后,我重新编写了 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": []
          }
      ]
      }
      

      【讨论】:

        【解决方案4】:

        我在使用 Git Bash 作为 VS Code 的默认 shell 时遇到了这个问题。将默认 shell 切换到 命令提示符,运行 npm install typescript 以供后代使用,然后重新构建。

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

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

        【讨论】:

          【解决方案5】:

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

          error TS5014: Failed to parse file '&lt;myprojloc&gt;/tsconfig.json/tsconfig.json': Unexpected token u in JSON at position 0.

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

          【讨论】:

          • 我不知道出了什么问题,但是当依赖 typescript 而不是 devDepnedency 导致上述错误时。
          • 三重检查 JS 文件路径然后运行 ​​npm install typescript --save-dev 已解决问题,谢谢。由于某种原因,输出路径已恢复为默认值。 找不到带有 TSC 的模块。
          【解决方案6】:

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

          npm install typescript
          

          应该解决这个问题。

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

          【讨论】:

          • 这解决了我的问题。虽然我很确定我以前安装过 typescript。
          • 有趣的是错误消息说它无法解析 JSON 字符串,但没有关于缺少 TypeScript ?
          【解决方案7】:

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

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

          【讨论】:

            猜你喜欢
            • 2016-09-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-11-21
            • 1970-01-01
            • 1970-01-01
            • 2019-08-30
            • 1970-01-01
            相关资源
            最近更新 更多