'TypeScript error TS5014: Unexpected token u in JSON at position 0

I am trying to compile a .ts to .js

I have tsconfig.json as below

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

below is my 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

{
// 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



Solution 1:[1]

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.

Solution 2:[2]

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. It was looking at <myprojloc>/tsconfig.json/ instead of <myprojloc>/. See how tsconfig.json is repeated twice in the error?

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.

Solution 3:[3]

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. You can then transfer your specific configuration into the newly generated tsconfig.json file, uncommenting the parts you care about.

If it persists after that, it could be an actual bug in the TypeScript version you're on.

Solution 4:[4]

I ran into this issue while using Git Bash as VS Code's default shell. Switch the default shell to Command Prompt, run npm install typescript for posterity, and build again.

I am using the "default" tsc build task:

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

Solution 5:[5]

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. Maybe a comma or a comment..

Solution 6:[6]

For my case, I had typescript but the pbm was my tsconfig.json got some trailling comma , on one last item. Remove trailling commas in that file

enter image description here

Some related thread: https://github.com/nrwl/nx/issues/1462#issuecomment-524878788

Solution 7:[7]

No need to uninstall tsc, just use npm install typescript at the project root level

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 obiwanjacobi
Solution 2 Alex Hui
Solution 3 Madara's Ghost
Solution 4 StCleezy
Solution 5 Dharman
Solution 6 4givN
Solution 7 bogi