Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 409
Uthistran Selvaraj Avatar asked Aug 21 '17 05:08

Uthistran Selvaraj


People also ask

How do I fix unexpected token a JSON at position 0?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What does this mean Syntaxerror unexpected token in JSON at position 0?

Re: Unexpected token in JSON at position 0 This usually means that an error has been returned and that's not valid JSON. Check the browser developer tools console and network tabs. Turn on Debugging and (after reproducing the error) check the web server error logs.


2 Answers

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.

like image 79
obiwanjacobi Avatar answered Sep 20 '22 09:09

obiwanjacobi


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.

like image 36
Alex Hui Avatar answered Sep 21 '22 09:09

Alex Hui