Last updated: Feb 29, 2024
Reading time·3 min
The error "File name differs from already included file name only in casing" occurs when we have two files with the same name, but different casing or rename a file and our IDE glitches.
To solve the error, make sure no two files only differ in casing and restart your IDE.
Here is an example of how the error occurs.
// Error: File name '/home/borislav/Desktop/typescript/src/Index.ts' // differs from already included file name // '/home/borislav/Desktop/typescript/src/index.ts' only in casing. import { country } from './Index'; console.log(country);
This is a file called index.ts
which imports from a file called Index.ts
(capital I
) in the same directory.
myFile.ts
and MyFile.ts
.Another common cause of the error is when we rename a file to use a different casing and our IDE glitches (VSCode often needs a restart).
myFile.ts
to MyFile.ts
, your code editor might not be able to tell that it's the same file.Do a full restart of your IDE, as reloading the VSCode window doesn't get the job done most of the time.
After a restart, your IDE should be able to pick up that there aren't two files with the same name in the same directory.
If that doesn't help try restarting your TS server as well.
The error also occurs if you have an import path with the wrong casing.
import { country } from './myFile'; import { country } from './MyFile';
The two import statements do the same on Windows machines because file names on
Windows
are case-insensitive.
However, if you run this code on a Unix-like operating system like Linux or macOS, you would get an error because they treat file names case-sensitively and these are 2 completely different files.
Make sure the path in your import statement doesn't have an incorrect casing.
placeholder.ts
and then renaming it back to the original name.I've written a detailed guide on how to import values from another file in TS.
tsconfig.json
Sometimes this helps the IDE realize that there aren't two files with the same name, but inconsistent casing.
A best practice is to use the forceConsistentCasingInFileNames
option in your
tsconfig.json file.
{ "compilerOptions": { "forceConsistentCasingInFileNames": true, // ... } }
When the
forceConsistentCasingInFileNames
option is set to true
, TypeScript will issue an error if you try to include a
file by using a different casing from the casing of the file name on the disk.
For example, if you try to import from MyFile.ts
, but the name of the file is
myFile.ts
, you'd get an error.
false
, TypeScript follows the case sensitivity rules of the file system it's running on.This can be very confusing if some of the developers on your team use Windows
machines and other Unix-like OS, because if you try to import myFile.ts
by
specifying MyFile.ts
, it will be found on Windows, but not on macOS or Linux.
You can learn more about the related topics by checking out the following tutorials: