Skip to content

An invalid JSON tsconfig.json give a bad error message  #7172

@davire

Description

@davire

TypeScript Version:
1.7.5

Code

When running tsc is an empty project folder with a tsconfig.json file such as :

{
    "version": "1.7.5",
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "outDir": "built",
        "rootDir": ".",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true
        // comment
    },
    "exclude": [
        "node_modules",
        "client"   // another comment
    ]
}

note that there are comments in that json file, which makes it invalid but it's easy to miss that especially if your code editor doesn't highlight it as an error but as a comment.

tsc will exit with an error that, as a beginner, couldn't figure out.

/data/code/tsrest$ tsc
/usr/lib/node_modules/typescript/lib/tsc.js:31084
            var jsonOptions = json["compilerOptions"];
                                  ^

TypeError: Cannot read property 'compilerOptions' of undefined
    at getCompilerOptions (/usr/lib/node_modules/typescript/lib/tsc.js:31084:35)
    at Object.parseJsonConfigFileContent (/usr/lib/node_modules/typescript/lib/tsc.js:31074:22)
    at parseConfigFile (/usr/lib/node_modules/typescript/lib/tsc.js:31351:40)
    at performCompilation (/usr/lib/node_modules/typescript/lib/tsc.js:31362:45)
    at Object.executeCommandLine (/usr/lib/node_modules/typescript/lib/tsc.js:31336:9)
    at Object.<anonymous> (/usr/lib/node_modules/typescript/lib/tsc.js:31635:4)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)

Searching on the faq and google didn't help and I eventually posted a stack overflow question http://stackoverflow.com/questions/35526404/tsc-ignores-my-tsconfig-json-file/35533849#35533849
My assuption was that tsc, or node, or npm was badly installed, or that I had a path problem.
I eventually figured out that tsc couldn't parse my tsconfig.json because of the comments in it.

Expected behavior:
tsc should exit with an error that makes it clear that it was unable to parse the tsconfig.json file.

Actual behavior:
displays an error message that makes sense only when you already know what the problem is.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions