# @ts-morph/bootstrap

> API for getting quickly set up with the TypeScript Compiler API.

Latest version **0.29.0** (published 2026-04-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @ts-morph/bootstrap
pnpm add @ts-morph/bootstrap
yarn add @ts-morph/bootstrap
bun add @ts-morph/bootstrap
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.29.0 |
| Published | 2026-04-12 |
| First published | 2019-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 47.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 6230 |
| Author | David Sherret |
| Maintainers | dsherret |
| Keywords | typescript, compiler, bootstrap |

## Links

- npm: https://www.npmjs.com/package/@ts-morph/bootstrap
- Repository: https://github.com/dsherret/ts-morph
- Homepage: https://github.com/dsherret/ts-morph#readme
- Issues: https://github.com/dsherret/ts-morph/issues
- npm.io page: https://npm.io/package/@ts-morph/bootstrap

## Dependencies (1)

- [@ts-morph/common](https://npm.io/package/@ts-morph/common.md) ~0.29.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.29.0 (latest) — 2026-04-12
- 0.28.1 — 2025-10-12
- 0.28.0 — 2025-09-07
- 0.27.0 — 2025-05-19
- 0.26.1 — 2025-02-05
- 0.26.0 — 2024-12-30
- 0.25.0 — 2024-10-06
- 0.24.0 — 2024-06-23
- 0.23.0 — 2024-03-10
- 0.22.0 — 2023-12-02
- 0.21.0 — 2023-09-21
- 0.20.0 — 2023-06-15
- 0.19.0 — 2023-03-28
- 0.18.0 — 2022-11-20
- 0.17.0 — 2022-09-03
- … 23 more at https://npm.io/package/@ts-morph/bootstrap/versions

## README

# @ts-morph/bootstrap

[![npm version](https://badge.fury.io/js/%40ts-morph%2Fbootstrap.svg)](https://badge.fury.io/js/%40ts-morph%2Fbootstrap)
[![CI](https://github.com/dsherret/ts-morph/workflows/CI/badge.svg)](https://github.com/dsherret/ts-morph/actions?query=workflow%3ACI)

A library for quickly getting set up with the [TypeScript](https://github.com/Microsoft/TypeScript) Compiler API.

This library is separate from [ts-morph](https://github.com/dsherret/ts-morph/blob/latest/packages/ts-morph/), but uses some of its underlying infrastructure.

- [Declarations](https://github.com/dsherret/ts-morph/blob/latest/packages/bootstrap/lib/ts-morph-bootstrap.d.ts)

## Example

```ts
import { createProject, ts } from "@ts-morph/bootstrap";

const project = await createProject(); // or createProjectSync

// these are typed as ts.SourceFile
const myClassFile = project.createSourceFile(
  "MyClass.ts",
  "export class MyClass { prop: string; }",
);
const mainFile = project.createSourceFile(
  "main.ts",
  "import { MyClass } from './MyClass'",
);

// ts.Program
const program = project.createProgram();
// ts.TypeChecker
const typeChecker = program.getTypeChecker();
// ts.LanguageService
const languageService = project.getLanguageService();
// ts.ModuleResolutionHost
const moduleResolutionHost = project.getModuleResolutionHost();
```

## Setup

Generally:

```ts
const project = await createProject({ tsConfigFilePath: "tsconfig.json" });
```

Or use the synchronous API:

```ts
const project = createProjectSync({ tsConfigFilePath: "tsconfig.json" });
```

### File Systems

```ts
// will use a real file system
const project = await createProject();

// in memory file system
const project2 = await createProject({ useInMemoryFileSystem: true });

// custom file system
const fileSystem: FileSystemHost = { ...etc... };
const project = await createProject({ fileSystem });
```

To access the file system after creating a project, you can use the `fileSystem` property:

```ts
project.fileSystem.writeFileSync("MyClass.ts", "class MyClass {}");
```

### Compiler options

```ts
const project = await createProject({
  compilerOptions: {
    target: ts.ScriptTarget.ES3,
  },
});
```

### tsconfig.json:

If you would like to manually specify the path to a tsconfig.json file then specify that:

```ts
const project = await createProject({
  tsConfigFilePath: "packages/my-library/tsconfig.json",
});

// output all the source files that were added
console.log(project.getSourceFiles().map(s => s.fileName));
```

_Note:_ You can override any tsconfig.json options by also providing a `compilerOptions` object.

For your convenience, this will automatically add all the associated source files from the tsconfig.json. If you don't wish to do that, then you will need to explicitly set `skipAddingFilesFromTsConfig` to `true`:

```ts
const project = await createProject({
  tsConfigFilePath: "path/to/tsconfig.json",
  skipAddingFilesFromTsConfig: true,
});
```

### Custom Module Resolution

Custom module resolution can be specified by providing a resolution host factory function. This also supports providing custom type reference directive resolution.

For example:

```ts
import { createProject, ts } from "@ts-morph/bootstrap";

// This is deno style module resolution.
// Ex. `import { MyClass } from "./MyClass.ts"`;
const project = await createProject({
  resolutionHost: (moduleResolutionHost, getCompilerOptions) => {
    return {
      resolveModuleNames: (moduleNames, containingFile) => {
        const compilerOptions = getCompilerOptions();
        const resolvedModules: ts.ResolvedModule[] = [];

        for (const moduleName of moduleNames.map(removeTsExtension)) {
          const result = ts.resolveModuleName(
            moduleName,
            containingFile,
            compilerOptions,
            moduleResolutionHost,
          );

          if (result.resolvedModule)
            resolvedModules.push(result.resolvedModule);
        }

        return resolvedModules;
      },
    };

    function removeTsExtension(moduleName: string) {
      if (moduleName.slice(-3).toLowerCase() === ".ts")
        return moduleName.slice(0, -3);
      return moduleName;
    }
  },
});
```

## Adding Source Files

Use the following methods:

- `const sourceFiles = await project.addSourceFilesByPaths("**/*.ts");` or provide an array of file globs.
- `const sourceFile = await project.addSourceFileAtPath("src/my-file.ts");` or use `addSourceFileAtPathIfExists(filePath)`
- `const sourceFiles = await project.addSourceFilesFromTsConfig("path/to/tsconfig.json")`

Or use the corresponding `-Sync` suffix methods for a synchronous API (though it will be much slower).

## Creating Source Files

Use the `Project#createSourceFile` method:

```ts
const sourceFile = project.createSourceFile("MyClass.ts", "class MyClass {}");
```

## Updating a Source File

Use the `Project#updateSourceFile` method. This can be provided a file path and string for the text or a new `ts.SourceFile` object:

```ts
const newSourceFile = project.updateSourceFile("MyClass.ts", "class MyClass {}");
// or
project.updateSourceFile(newSourceFileObj);
```

## Removing a Source File

Use the `Project#removeSourceFile` method:

```ts
project.removeSourceFile("MyClass.ts");
// or
project.removeSourceFile(sourceFile);
```

## Formatting Diagnostics

```ts
import { createProject, ts } from "@ts-morph/bootstrap";

const project = await createProject({ useInMemoryFileSystem: true });
project.createSourceFile("test.ts", "const t: string = 5;");

const program = project.createProgram();
const diagnostics = ts.getPreEmitDiagnostics(project.createProgram());

console.log(project.formatDiagnosticsWithColorAndContext(diagnostics));
```

---
_Source: https://npm.io/package/@ts-morph/bootstrap · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
