'rootDir' is expected to contain all source files

I have a typescript project and faced an issue complaining about imports with the same error. I have solved it by correcting the value of include in tsconfig.json file as below

{
  "compilerOptions": {
    "module": "CommonJS",
    "target": "ES2017",
    "noImplicitAny": true,
    "preserveConstEnums": true,
    "outDir": "./dist",
  },
  "exclude": ["node_modules", "**/*.test.ts"],
  "include": ["src"]
}

Hope this may help.


I had the same issue, but the solution of @Agius did not help.

I had:

Angular Workspace
  - projects
      - lib1
      - lib2
  - src
      - test application

In fact I had moved a component from lib2 to lib1, by dragging the folder in WebStorm. By doing this, the references in lib2 to that component were not removed but updated and pointed to the source-folder of lib1. I forgot to remove these references which were no longer needed in lib2. After removing all references to the component in lib2, that library compiled.

I had to remove the references in

  • public_api.ts
  • ./lib/lib2.module.ts

Maybe there are more references in your project.


This looks like the problem that is occurring due to the import types which was introduced in TypeScript 2.9. When emitted these are not being rewired properly see line 3.

dist/bar/lib/bar.component.d.ts(5,11):

export declare class BarComponent implements OnInit {
    private barService;
    list: import("projects/bar/src/lib/types").Item[]; 
    constructor(barService: BarService);
    ngOnInit(): void;
}

In the above emitted dts, list: import("projects/bar/src/lib/types").Item[]; should be something like import("./types").Item[]; instead.

A workaround for this can be that from your code instead infering the type, you explicitly set it.

in bar.component.ts change the below;

list = this.barService.list();

to:

list: Item[] = this.barService.list();

This will remove the type import and the consuming library will build.

I also checked a bit with future versions of TypeScript, it is still an issue in TypeScript 3.0.1, but it looks like it has been addressed in dev version of TypeScript 3.1.0, ie 3.1.0-dev.20180813