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

I am trying to compile .ts in .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 automatically generated tasks.json looks 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 get the following 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 am I doing wrong? Please note that I added versions in package.json

+5
json javascript typescript typescript-compiler-api
source share
5 answers

After receiving advice from colleagues and trying something from this link , I rewrite tasks.json as shown below and now it works. The team seems to have some problem earlier

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

I would like to add that you also see this error when you forgot to add 'typescript' as a dependency.

 npm install typescript 

must fix it.

Please note that this dependency present in the .json package in question.

+15
source share

There could be many things that could go wrong if you save a file that will prevent proper parsing. I usually prefer not to do this by renaming the file to tsconfig.json.backup or something like that, then calling tsc --init to create a known good file. You can then transfer your specific configuration to the newly created tsconfig.json file, uncommenting the details you are interested in.

If after that it is saved, it could be the actual error in the TypeScript version you are in.

+3
source share

If you look closely at the error message, you will see the reason for the failure. The command line created to run tsc looks for the wrong directory. He looked at <myprojloc>/tsconfig.json/ instead of <myprojloc>/ . See how tsconfig.json is repeated twice in 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 see how editing the task and telling command to search in the correct directory for tsconfig.json would also solve the problem.

+2
source share
 npm install typescript@latest --save-dev 

corrects this.

0
source share

All Articles