TypeScript Config File Parser

3.1.6 · active · verified Sun Apr 19

tsconfck is a robust utility designed to find and parse `tsconfig.json` or `jsconfig.json` files programmatically, abstracting away the complexities of TypeScript's native parsing mechanisms. It enables developers to work with TypeScript configuration files without requiring a direct dependency on the `typescript` package itself, offering a lightweight alternative. The current stable version is 3.1.6, with a release cadence that appears to be frequent patch releases addressing bug fixes and minor improvements, as seen in the recent changelog for 3.1.x. Key differentiators include its ability to resolve `extends` and `references` properties, optional caching for performance, a minimal bundle size (4.8KB gzip), and being completely asynchronous. It also offers `parseNative` for when the `typescript` peer dependency *is* present and desired for official API usage. It's notably used by popular tools like Vite and Astro for their configuration needs.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to find and parse a tsconfig.json file using the `parse` function from `tsconfck` based on a source file path. It creates a temporary project structure, parses its config, and then cleans up.

import { parse } from 'tsconfck';
import * as path from 'path';
import * as fs from 'fs/promises';

// Create a dummy tsconfig.json for demonstration
const projectRoot = path.join(process.cwd(), 'temp_tsconfck_project');
const tsconfigFile = path.join(projectRoot, 'tsconfig.json');

async function setupProject() {
  await fs.mkdir(projectRoot, { recursive: true });
  await fs.writeFile(tsconfigFile, JSON.stringify({
    "compilerOptions": {
      "target": "es2020",
      "module": "esnext",
      "strict": true,
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "skipLibCheck": true
    },
    "include": [
      "src/**/*.ts"
    ]
  }, null, 2));
  await fs.mkdir(path.join(projectRoot, 'src'), { recursive: true });
  await fs.writeFile(path.join(projectRoot, 'src', 'index.ts'), 'console.log("Hello");');
}

async function cleanupProject() {
  await fs.rm(projectRoot, { recursive: true, force: true });
}

async function run() {
  await setupProject();
  try {
    const { tsconfigFile, tsconfig, extended, solution, referenced } = await parse(path.join(projectRoot, 'src', 'index.ts'));

    console.log('Found tsconfig file:', tsconfigFile);
    console.log('Parsed tsconfig (merged):', tsconfig);
    // console.log('Extended configs:', extended);
    // console.log('Solution config:', solution);
    // console.log('Referenced configs:', referenced);
  } catch (error) {
    console.error('Error parsing tsconfig:', error);
  } finally {
    await cleanupProject();
  }
}

run();

view raw JSON →