If you are having trouble with the “Cannot write file # because it would overwrite input file” error in TypeScript, then this article is for you. Let’s figure out the solution to this problem as well as the reason why the error occurs in your project.
Why do we get this error message?
The error “Cannot write file # because it would overwrite input file” error in TypeScript happens because, in the tsconfig.json file, we did not indicate the ‘outDir‘ directory inside the ‘exclude‘ array.
Sometimes the error can also happen because you import a file from the specified ‘outDir’ directory or use Monorepos in your project.
Solution for “Cannot write file # because it would overwrite input file” error in TypeScript
To get around this error, open the file ‘tsconfig.json’ and check if your ‘outDir’ is in the array ‘exclude‘ or not. You must ensure the project’s build directory is excluded. To illustrate, your ‘tsconfig.json’ should be like this:
{
"compilerOptions": {
"outDir": "build",
},
"exclude": ["node_modules", "build/**/*"]
//…other options
}
If the array ‘exclude’ does not contain the value you declared for the ‘outDir’ option, modify it like in the example above. The build directory is specified for TypeScript to output emitted files in this example.
After reconfiguring the tsconfig.json file, restart the IDE as well as the development server so that the system can correctly update the latest changes.
In addition, you also need to make sure you don’t import any function or anything else from your ‘outDir’ directory. For example:
import { firstFunc } from "../../../build/myFolder/FirstFunction";
//or
import { secondFunc } from "../../../dist/myFolder/SecondFunction";
In the above example, we describe the case where you import a function from the directory ‘build’/ ‘dist’, which are the two most commonly given names for the ‘outDir’ option. It may be different as you work on your project, leaning on how you name it.
The last thing to keep in mind to avoid getting errors “Cannot write file # because it would overwrite input file” is to ensure that there are no circular dependencies in your project. Here is an example:
In Home.ts:
import { Contact } from "./About";
export interface WelcomeMessage {
message(s: string): string;
}
In About.ts:
import { WelcomeMessage } from "./Home";
export interface Contact {
email(s: string): string;
}
It can be seen that two files, ‘Home.ts’ and ‘About.ts’, are interdependent. Because of ‘Home.ts’ imports from ‘About.ts’ and vice versa. This will cause trouble for your project, especially when you use Monorepos.
Summary
To sum up, the necessary information about “Cannot write file # because it would overwrite input file” error in TypeScript has been conveyed by us through this article. Thanks for being so interested, and good luck with your upcoming projects.
Maybe you are interested:
- “Augmentations for global scope can only be directly nested” in Typescript
- File is not under ‘rootDir’ error in TypeScript
- Property ‘files’ does not exist on type ‘EventTarget’ in TypeScript
My name’s Christopher Gonzalez. I graduated from HUST two years ago, and my major is IT. So I’m here to assist you in learning programming languages. If you have any questions about Python, JavaScript, TypeScript, Node.js, React.js, let’s contact me. I will back you up.
Name of the university: HUST
Major: IT
Programming Languages: Python, JavaScript, TypeScript, Node.js, React.js