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

后端 未结 6 836
南方客
南方客 2021-01-07 19:11

I am trying to compile a .ts to .js

I have tsconfig.json as below

{
\"compilerOptions\": {
    \"target\": \"es5\",
    \"module\": \"co         


        
相关标签:
6条回答
  • 2021-01-07 19:21

    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
            }
          }
        ]
     }
    
    0 讨论(0)
  • 2021-01-07 19:26

    After trying something from this link, I re-write the tasks.json as below and it now worked. Seems the command has some problem previously

    {
    // 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 讨论(0)
  • 2021-01-07 19:27

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

    0 讨论(0)
  • 2021-01-07 19:30

    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.

    0 讨论(0)
  • 2021-01-07 19:31

    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.

    0 讨论(0)
  • 2021-01-07 19:33

    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.

    0 讨论(0)
提交回复
热议问题