Adding Source Files

You will need to populate the project object with source files.

By a tsconfig.json

Source files will be added when instantiating with a tsConfigFilePath:

import { Project } from "ts-morph";

const project = new Project({
  tsConfigFilePath: "path/to/tsconfig.json",

...and this can be disabled by setting skipAddingFilesFromTsConfig: true:

const project = new Project({
  tsConfigFilePath: "path/to/tsconfig.json",
  skipAddingFilesFromTsConfig: true,

Alternatively, populate the project object by calling addSourceFilesFromTsConfig:


Source File Dependency Resolution

By default, all the source files added to the project in the constructor via a tsconfig.json will automatically be analyzed to include the source files they depend on. If you wish to skip this analysis step, then provide the skipFileDependencyResolution option:

const project = new Project({
  tsConfigFilePath: "path/to/tsconfig.json",
  skipFileDependencyResolution: true,

If you are adding source files to a project in other ways and want to ensure the all the source files depended on by the added source files are also included in the Project, then call the .resolveSourceFileDependencies() after adding everything:

const project = new Project();

// add everything to the project

// optionally call this when complete to resolve and
// add the dependent source files to the project

By file globs or file paths

Specify as many file globs or file paths as you wish:

project.addSourceFilesAtPaths(["folder/file.ts", "folder/otherFile.ts"]);
project.addSourceFilesAtPaths(["**/*.ts", "!**/*.d.ts"]);

By file path

const sourceFile = project.addSourceFileAtPath("path/to/file.ts"); // or addSourceFileAtPathIfExists

By structure

Create source files based on an object that looks like the AST of a source file:

const sourceFile = project.createSourceFile("path/to/myStructureFile.ts", {
  statements: [{
    kind: StructureKind.Enum,
    name: "MyEnum",
    members: [{
      name: "member",
  }, {
    kind: StructureKind.Class,
    name: "MyClass",
    // etc...
  // etc...

The above would create a source file with the following text:

enum MyEnum {

class MyClass {

By string

const fileText = "enum MyEnum {\n}\n";
const sourceFile = project.createSourceFile("path/to/myNewFile.ts", fileText);

By writer function

const sourceFile = project.createSourceFile("path/to/myOtherNewFile.ts", writer => {
    .writeLine("import * as ts from 'typescript';").blankLine()
    .writeLine("export class MyClass {}");


createSourceFile will throw an error if the file already exists. To not throw an error, set the overwrite option to true.

const sourceFile = project.createSourceFile("path/to/myNewFile.ts", "", { overwrite: true });


Adding source files to the project from a structure, writer function, or text will act like any other source file, but they will not be saved to the disk unless you ask it to be.

// save it to the disk if you wish:
await sourceFile.save(); // or saveSync();