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

Javascript TypeScript错误TS5014:JSON中位置0处出现意外标记u,javascript,json,typescript,Javascript,Json,Typescript,我正在尝试将a.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

我正在尝试将a.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.
正在执行任务:\node\u modules\.bin\tsc.cmd-p“\tsconfig.json”<
错误TS5014:未能分析文件“/tsconfig.json/tsconfig.json”:json中的意外标记u位于位置0。
终端将被任务重用,请按任意键将其关闭。

我做错了什么?请注意,我已经在
package.json中添加了版本,在保存文件时可能会出现一些问题,从而妨碍正确的解析。我通常选择不处理它,将文件重命名为
tsconfig.json.backup
或其他文件,然后调用
tsc--init
生成一个已知良好的文件。然后,您可以将特定配置传输到新生成的
tsconfig.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"
}
}

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

在尝试了一些这方面的内容之后,我重新编写了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": []
    }
]
}

我想指出的是,当您忘记添加“typescript”作为依赖项时,您也会看到这个错误

npm install typescript
应该解决这个问题


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

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

错误TS5014:未能分析文件“/tsconfig.json/tsconfig.json”:json中的意外标记u位于位置0。


运行
npm install typescript--save dev
对我来说很有效,但是我可以看到编辑任务并指定
命令来查找tsconfig.json的正确目录将如何解决这个问题。

我在使用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
        }
      }
    ]
 }

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

也许您在文件开头有BOM[我保存了该文件,但没有BOM。仍然相同@Felixalso添加了“emitBOM”:tsconfig.json中为false。没有区别这解决了我的问题。虽然我非常确定我以前安装过typescript。有趣的是,错误消息说它无法解析json字符串,但typescript缺失的原因我不知道是什么问题,但当typescript处于依赖项而不是DevDependence时,导致了上述错误。三重检查运行
npm install typescript--save dev
的JS文件路径已修复此问题,谢谢。由于某些原因,输出路径已恢复为默认路径。找不到带有TSC的模块。