Set TypeScript compiler's base/working directory
See original GitHub issueIt looks like TypeScript compiler resolves files relative to the location of the tsconfig.json
file (however, I couldn’t find anything about paths resolution in the official documentation).
Is there a way to specify an alternative base/working directory, which will be used for relative paths resolution?
I want to use a generic tsconfig.json
file to compile multiple projects in various directories (one at a time).
P/S: I’ve created a question on StackOverflow first, but haven’t received any attention there, so I’ve decided to ask here directly.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:27
- Comments:13 (2 by maintainers)
Top Results From Across the Web
Set TypeScript compiler's base/working directory
Typescript compiler automatically determines the greatest common ancestor directory, hence rootDir , of all *.ts files according to your ...
Read more >TSConfig Reference - Docs on every TSConfig option
Lets you set a base directory to resolve non-absolute module names. ... When declaration is set to true , running the compiler with...
Read more >Configuring TypeScript compiler - inDepthDev
It specifies the base directory to resolve non-relative modules in. If paths option is set, the compiler goes through folders defined in paths...
Read more >Exploring advanced compiler options in TypeScript
json files. The TypeScript compiler can reference a tsconfig.json file in another directory when compiling code in the current directory ...
Read more >TypeScript tutorial with Visual Studio Code
You will need to install the TypeScript compiler either globally or in your workspace to transpile ... Create a new folder HelloWorld and...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@RyanCavanaugh One problem is that
rootDir
orbaseUrl
have no effect onincludes
andexclude
paths.includes
andexclude
seem to always be relative to thetsconfig.json
file, regardless of where it is.If there was a new top-level option (f.e.
projectDir
or something), then this could point to some arbitrary location, and all other options (rootDir
,rootDirs
,baseUrl
,outDir
,include
, andexclude
would all be relative to thisprojectDir
setting).This would then make it feasible that in each project’s tsconfig, we could do something simple like
and at that point any paths in
node_modules/my-configs/tsconfig.json
will always work due to theprojectDir
being specified. Thetsconfig
file could be located anywhere, and it would just work.As @octogonz mentioned, being able to specify module identifiers in
extends
would be convenient too.Thank you Ryan for a response.
But, according to the docs, the
rootDir
option affects only the output directory structure and thebaseUrl
option is for non-relative modules (likenode_modules
I guess?).I will try to explain better. I have multiple projects with very similar directory structure and compilation requirements. I would like to define compilation options (including relative paths to source files from the root of the project directory) in a single place. Therefore I have a single
tsconfig.json
file, which I want to use to compile multiple projects. However, the compiler is trying to resolve the source files, specified in the config, not from the project root directory, but from the directory, where this generictsconfig.json
file is located.Consider, I have the following generic
tsconfig.json
file in~/common/tsconfig.json
:And I have multiple projects like
~/project1
,~/project2
, etc. I want to run the compiler like this:tsc --project ~/common/tsconfig.json --projectRoot ~/project1
and I want it to start compilation using files, located in~/project1/src/**/*.ts
and not in~/common/src/**/*.ts
.Also, I don’t want to use
extends
functionality of thetsconfig.json
, because I want projects to be agnostic from the location of the generic config.I hope it makes sense.