Resolving tsconfig.json using TypeScript.

Recently, while working on a project, I came across a use case, where in I needed to resolve the tsconfig.json and read the option moduleResolution from the config. I tried looking up this in the official docs, but could not find anything. After scavenging the internet, I came across this gist, which gave me the solution. I’ll try to explain the solution as best as I can.

Turns out, this is a two step process. In first step, you resolve the configuration file by using the findConfigFile method and in second step you read the file (from first step) using readConfigFile method. Below is the code for the same:

import ts from 'typescript';

const currentDir = process.cwd();
const configFile = ts.findConfigFile(currentDir, ts.sys.fileExists, 'tsconfig.json');

if (configFile) {
  const { config } = ts.readConfigFile(configFile, ts.sys.readFile);
  // config will be the resolved object
}

Example

I have created an example on StackBlitz. It has the follwing directory structure:

|- test/
|    |- tsconfig.json
|- index.js
|- package.json
|- tsconfig.json

And following content:

// index.js
import ts from 'typescript';

const currentDir = process.cwd();
const configFile = ts.findConfigFile(currentDir, ts.sys.fileExists, 'tsconfig.json');

console.log('configFile =>', configFile);

if (configFile) {
  const { config } = ts.readConfigFile(configFile, ts.sys.readFile);

  console.log('moduleResolution =>', config.compilerOptions.moduleResolution);
}
// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "esnext",
    "moduleResolution": "NodeNext"
  }
}
// test/tsconfig.json
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "moduleResolution": "Node"
  }
}

Now if you run node index.js in the project’s root directory, you’ll see that moduleResolution is logged as NodeNext.

And if you run node ../index.js from inside the test directory, you’ll see that moduleResolution is logged as Node.

This works as expected.

cli output

You can try it below:

typescript