Warning: session_start(): open(/tmp/sess_b29baec50e4c1125c0f8ce0ee001c3af, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
"File # is not under 'rootDir'" error in TypeScript - How to fix it? - LearnShareIT

“File # is not under ‘rootDir'” error in TypeScript – How to fix it?

File is not under 'rootDir' error in TypeScript

In this post, we talk about the “File # is not under ‘rootDir'” error in TypeScript. Let’s see what this error message means and how to solve this problem.

Why do we get this error?

The reason we get the “File # is not under ‘rootDir‘” error message in TypeScript is that we are trying to import a component (or anything else) from a file that is not located under the root directory of your project. In TypeScript, the ‘rootDir‘ option in tsconfig.json allows you to config your project’s root directory.

Suppose here is your project structure and the error happens:

TStutorial
  └──src
     └── index.ts
     └── home.ts
  └──  about.ts

In main.ts:

import { Home} from '../home';
import { About } from '../about';  //error occurs here
// … other lines of code

Output:

Error: File '/Users/Admin/Desktop/TStutorial/about.ts' is not under 'rootDir' '/Users/Admin/Desktop/TStutorial/src'. 'rootDir' is expected to contain all source files.

In this example, the ‘rootDir‘ option is set to the ‘src‘ folder. So this error message in TypeScript means that all the files should be located under the ‘src‘ folder.

Methods to fix the “File # is not under ‘rootDir'” error in TypeScript

We share with you two ways to help you fix this error. Let’s see what these methods are:

Change the file location in your project

The simplest and easiest way to solve this problem is to move all your files inside the root directory before you want to import them.

Continuing with the example above, let’s change it a bit. The directory structure now will look like this:

TStutorial
  └──src
     └── index.ts
     └── home.ts
     └──  about.ts

As you can see, all the files have been moved inside the ‘src‘ folder, which the ‘rootDir‘ option is pointing to. Now you can import between files without worrying about errors.

import { Home} from '../home';
import { About } from '../about';  //the error no longer occurs here
// … other lines of code

Remove the ‘rootDir’ option in tsconfig.json

If you don’t want to change the folder structure in your project, then remove the option ‘rootDir’ in the ‘tsconfig.json‘ file. For instance, here is your ‘tsconfig.json‘ file:

{
  "compilerOptions": {
    "rootDir": "src", //you can try to remove this line
    // ... other options
  }
}

At this point, the TypeScript compiler will automatically implicitly set the ‘rootDir‘ option for your project by following your imports.

Summary

In summary, there are two ways for you to fix the “File # is not under ‘rootDir'” error in TypeScript. You can try to apply one of the methods we provide in this article. Thank you for reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *