TypeScript错误TS5014:位于0的JSON中出现意外的标记u

Uth*_*raj 8 javascript json typescript typescript-compiler-api

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

我有tsconfig.json如下

{
"compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "outFile": "build/test.js"
},
"exclude": [
    "node_modules"
]
}
Run Code Online (Sandbox Code Playgroud)

下面是我的 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"
}
}
Run Code Online (Sandbox Code Playgroud)

并且自动生成的内容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
        }
    }
]
}
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

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

obi*_*obi 28

我想在你忘记添加'typescript'作为依赖项时也会看到这个错误.

npm install typescript
Run Code Online (Sandbox Code Playgroud)

应该解决这个问题

请注意,这种依赖性存在于问题的package.json.

  • 有趣的是错误消息说它无法解析JSON字符串,但是关于TypeScript却一无所获 (5认同)

小智 8

如果您仔细查看错误消息,您可以看到失败的原因。为运行而形成的命令行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 的正确目录中查找也可以解决问题。


Uth*_*raj 1

在尝试了此链接中的某些操作后,我重写了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": []
    }
]
}
Run Code Online (Sandbox Code Playgroud)