Why is the TypeScript compiler ignoring tsconfig.json?
Asked Answered
D

2

47

I have this file, pasted from a tutorial (and let's face it, the disparity between docs, tuts, and examples is astounding):

/scripts/tsconfig.json:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "commonjs",
        "noEmitOnError": true,
        "noImplicitAny": false,
        "outDir": "../wwwroot/appScripts/",
        "removeComments": false,
        "sourceMap": true,
        "target": "es5",
        "moduleResolution": "node"
    },
    "exclude": [
        "node_modules",
        "typings/index",
        "typings/index.d.ts"
    ]
}

Options are set to compile on save, but whenever I save a TypeScript file, the JavaScript output ends up 'under', or 'attached to', the source file:

TypeScript
|
--test.ts 
    |
    --test.js

, and that is physically in the same directory as the source, /TypeScript. If tsconfig.json is missing, the compiler complains, but when it's present, and it definitely is, the compiler ignores the "outDir": "../wwwroot/appScripts/" setting.

I am really to new to Gulp, but the Gulp task looks OK to me:

var tsProject = ts.createProject('scripts/tsconfig.json');
gulp.task('ts', function (done) {
    //var tsResult = tsProject.src()
    var tsResult = gulp.src([
            "scripts/*.ts"
    ])
        .pipe(ts(tsProject), undefined, ts.reporter.fullReporter());
    return tsResult.js.pipe(gulp.dest('./wwwroot/appScripts'));
});
Darkish answered 8/9, 2016 at 19:46 Comment(2)
What is ts? Is it this gulp typescript module? npmjs.com/package/gulp-typescriptStaciestack
What is watching for compile on save?Staciestack
S
72

Options are set to compile on save

When you save a file it is automatically compiling that single file and files imported on that file. Turn off auto compile option from your IDE, so compiler will consider tsconfig.json file.

When input files are specified on the command line, tsconfig.json files are ignored.

The presence of a tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project. A project is compiled in one of the following ways:

Using tsconfig.json

  1. By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain.

  2. By invoking tsc with no input files and a --project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

Samira answered 9/9, 2016 at 13:32 Comment(4)
Thanks for this answer! This was driving me crazy, until I remembered I had recently added '"compileOnSave": true' to my tsconfig.json.Lotus
I carefully specify all of these compiler options and then, in a fit of undocumented and weird behavior, the compiler ignores the compiler options... because I specified an input file? What?Epidemiology
Is there a logical reason why tsc ignores your config file when you specify files to compile? It's particularly annoying when you're trying to debug your config by testing it on a single file for speed.Californium
5 years old and still accurate. This should be the accepted answer.Constantia
W
19

One of the reasons can be explicitly specified file

Instead of

npx tsc index.ts

Run

npx tsc

By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json (c) TypeScript Documentation

Windowshop answered 4/6, 2022 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.