102

I am getting this fairly non-sensical tsc transpilation error:

error TS6059: File '/Users/alex/codes/interos/teros-cli/src/logging.ts' is not under 'rootDir' '/Users/alex/codes/teros/notifier-server/src'. 'rootDir' is expected to contain all source files.

my PWD is /Users/alex/codes/teros/notifier-server and the tsconfig.json file for /Users/alex/codes/teros/notifier-server/tsconfig.json is:

{ "compilerOptions": { "outDir": "dist", "allowJs": false, "pretty": true, "resolveJsonModule": true, "sourceMap": false, "skipLibCheck": true, "rootDir": "src", "declaration": false, "baseUrl": ".", "target": "es2018", "module": "commonjs", "noImplicitAny": true, "removeComments": true, "allowUnreachableCode": true, "lib": [ "es2017", "es2018" ] }, "compileOnSave": false, "include": [ "src" ] } 

this seems like a bug..since teros-cli dir is outside the PWD, and is governed by a separate tsconfig.json file.

I even changed this field to:

 "include": [ "/Users/alex/codes/teros/notifier-server/src" ], "exclude": [ "/Users/alex/codes/teros/teros-cli" ] 

still get the same error.

9 Answers 9

81

What is rootDir?

rootDir is set to a root folder, that contains all your source files. If not specified, TS will automatically choose a suitable parent folder of all inputs. rootDir also determines the output directory.

What does the error mean?

My guess is you have an import statement for logging.ts somewhere in notifier-server:

import {logger} from "@teros-cli/logging" // or similar 

Then logging.ts module will be automatically included by the compiler, regardless of include and exclude options in tsconfig.json. One way to check all included files is tsc --listFiles.

A tsconfig.json file outside notifier-server doesn't help here. The compiler picks up exactly one config per tsc compilation and optionally pulls inherited configs. If it cannot find one in notifier-server project root (where you started tsc), only then the compiler searches upwards the parent directory chain, until a config is found.

Possible solutions

One fix is to just remove "rootDir": "src" from compiler options, so it gets set automatically. Caution: rootDir will then consider both projects as inputs!

Alternative: You can add a separate logging.ts module contained in notifier-server/src project and drop the external import.

Hope, that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Another option is to use Project References. See this answer for dealing with rootDir issues and using Project Reference to manage interal dependencies. Things will start to make a lot more sense.
9

When using NX

I ended up here but none of the answers helped my case.

I'm using an NX monorepo, and the problem was that the "name" field of my library's package.json was set to the library's project name (same as project.json).

Apparently this was causing the issue and I had to rename it to the same path specified in the tsconfig.base.json

2 Comments

This did seem to fix my problem, but it meant to have "nice" imports, I had to set the name of the library to "@mycomp/x/y", which doesn't look very nice and I get a linting warning about the name in package.json. This doesn't seem to be a problem when including a library into an angular app.
I had the same problem while using NX and it turned out to be a wrong "name" value in my newly generated library's package.json file. Thanks!
5

I got this error by duplicating paths in tsconfigs. Removing the latter solved the issue:

// ./tsConfig.json "paths": { "@myPackage": ["./myPackage/index.ts"], }, // ./MY_OTHER_PACKAGE/tsConfig.json "paths": { "@myPackage": ["../myPackage/index.ts"] }, 

Comments

5

Possible Problems

1- Circular dependencies

2- Package name should be the same as the defined path.

// libs/utils/package.json { ... "name":"Should-Be-Same" ... } // tsconfig.json { ... "Should-Be-Same":["/libs/utils/lib/src/index.ts"] ... } 

3- Spelling error in the paths

2 Comments

I fixed the problem by following point number 2. My package name was not the same as tsconfig path. Thanks a lot !
I am so glad to be useful.
4

My solution is for angular libraries. For external libraries, I added the paths definition to the tsconfig.lib.json file.

tsconfig.lib.json

{ "extends": "../../../tsconfig.json", "compilerOptions": { "resolveJsonModule": true, "outDir": "../../../out-tsc/lib", "declaration": true, "declarationMap": true, "inlineSources": true, "types": [], "paths": { "@tbtk/popover": ["dist/tbtk/popover"] } }, "exclude": ["**/*.spec.ts"] } 

1 Comment

setting the path to the build folder solved it for me
4

For everyone landing here due to the following error:

error TS6059: File '/home/myuser/projects/myproject/apps/myapp/jest.config.ts' is not under 'rootDir' '/home/myuser/projects/myproject/apps/myapp/src'. 'rootDir' is expected to contain all source files. 

Most probably the rootDir in tsconfig is set to src with the include set to "**/*.ts".

Just add the jest.config.ts to the exclude array:

tsconfig.json

{ "extends": "../../tsconfig-base.json", "compilerOptions": { "rootDir": "src", "outDir": "out", ... }, "exclude": ["node_modules", "jest.config.ts"], // <-- add `jest.config.ts` here "include": ["**/*.ts"] } 

Comments

0
 -src index.ts -tsconfig.json 

If in tsconfig.json its "rootDir": "./src" & index.ts is in root it doesn't work, make sure index.ts is inside src

Comments

0

Using Nx Monorepo

The solution for me was to replace all the path re-mapping like this '@my-lib/services/persona-type.service' with relative paths like this '../../services/persona-type.service'.

I faced the same issue while building the libs in the nx monorepo. When I was building the my-lib2 the path in the dependency lib was impacting the building process.

I was using paths re-map like this: "@my-lib/": ["libs/my-lib/src/lib/"].

error TS6059: File '/var/www/app/libs/my-lib/src/lib/services/persona-type.service.ts' is not under 'rootDir' '/var/www/app/libs/my-lib2/src'. 'rootDir' is expected to contain all source files. 18 import { PersonaTypeService } from '@my-lib/services/persona-type.service'; 

1 Comment

this way (iirc) you lose graph stats. i switched from nx just because of this
0

Using Yarn Workspaces:

Add references.path to tsconfig.json for the package you are trying to import. Example from public repo https://github.com/graphile/starter/blob/main/%40app/server/tsconfig.json#L14

"references": [{ "path": "../config" }, { "path": "../graphql" }] 

Comments