question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set TypeScript compiler's base/working directory

See original GitHub issue

It 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:open
  • Created 5 years ago
  • Reactions:27
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
trusktrcommented, Feb 9, 2020

@RyanCavanaugh One problem is that rootDir or baseUrl have no effect on includes and exclude paths. includes and exclude seem to always be relative to the tsconfig.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, and exclude would all be relative to this projectDir setting).

This would then make it feasible that in each project’s tsconfig, we could do something simple like

{
  "extends": "./node_modules/my-configs/tsconfig.json",
  "projectDir": "./"
}

and at that point any paths in node_modules/my-configs/tsconfig.json will always work due to the projectDir being specified. The tsconfig 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.

11reactions
slavafomincommented, Jul 5, 2018

Thank you Ryan for a response.

But, according to the docs, the rootDir option affects only the output directory structure and the baseUrl option is for non-relative modules (like node_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 generic tsconfig.json file is located.


Consider, I have the following generic tsconfig.json file in ~/common/tsconfig.json:

{
    "compilerOptions": {
    },
    "include": [
        "index.ts",
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

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 the tsconfig.json, because I want projects to be agnostic from the location of the generic config.

I hope it makes sense.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found