If I run:
$ tsc foo.ts
I get the following error:
foo.ts(1,25): error TS1148: Cannot compile modules unless the '--module' flag is provided.
In the same directory is my tsconfig.json file:
{
"compilerOptions": {
"module": "es2015",
"target": "es2015"
},
"exclude": [
"node_modules"
]
}
And here is the file I'm trying to compile:
export default function foo() {
console.log('foo');
}
Now, if I manually provide the --module and --target flags, everything compiles as expected:
$ tsc --module es2015 --target es2015 foo.ts
Somehow, it's not picking up the tsconfig.json file compilerOptions?
$ tsc -v
message TS6029: Version 1.7.5
What is the expected behavior?
That it compiles without errors? If I manually provide the --module es2015 and --target es2015 flags in the command then it compiles fine, so why isn't it picking up the compilerOptions in my tsconfig.json file?
The problem is that tsconfig.json isn't considered when you manually specify your source files on the command line. It's certainly not great behavior, but I think it's by design since if you use the --project option, you'll get the following error:
error TS5042: Option 'project' cannot be mixed with source files on a command line.
Aha! Not very intuitive, but I suppose I can live with that. Still, I wonder if the error message could be improved to somehow indicate this to the user. Maybe whenever the tsc command is run, if it's run directly on files, an "info" message could show up that says something like, "Not using detected tsconfig.json". I think I'll open up an issue with this request. Thanks!
I also opened #6600 for you @jedmao.
@DanielRosenwasser
Is this a new change? Because VSCode stops compiling as per default instructions.
From Here https://code.visualstudio.com/Docs/languages/typescript
The first TypeScript example uses tsc (the TypeScript compiler) as the command to be executed. > The task we are working with looks something like this:
{ "version": "0.1.0", "command": "tsc", "isShellCommand": true, "showOutput": "silent", "args": ["HelloWorld.ts"], "problemMatcher": "$tsc" }
The args parameter causes the exact issue @jedmao pointed out
foo.ts(1,25): error : Cannot compile modules unless the '--module' flag is provided.
When I remove args everything is working! This is because the fileName is being supplied to tsc as a command line argument and it ignores tsconfig.json.
So, is the vscode doc wrong or is this a change in the recent versions of tsc?
So, is the vscode doc wrong or is this a change in the recent versions of tsc?
you can either pass file names to the compiler on the commandline, and the tsconfig.json will be ignored, or you can run tsc with no arguments and the tsconfig.json will be picked up automatically.
consider setting args to "--p <path to tsconfig.json>" instead.
Most helpful comment
The problem is that
tsconfig.jsonisn't considered when you manually specify your source files on the command line. It's certainly not great behavior, but I think it's by design since if you use the--projectoption, you'll get the following error: