Software Development

How can I configure tsc to strictly exclude the node_modules folder during compilation?

RA Asked by Rachel Stevens · 14-03-2025
0 upvotes 16,166 views 0 comments
The question

I'm currently working on a large TypeScript project and noticed that my build times are incredibly slow. It seems like the TypeScript compiler (tsc) is attempting to type-check or include files within the node_modules directory, leading to thousands of errors from third-party libraries that I don't control. I’ve tried adding "exclude" to my tsconfig.json, but I’m still seeing those files in my output or being scanned. What is the proper configuration to ensure the compiler completely ignores this folder?

3 answers

0
JE
Answered on 16-03-2025

To properly force tsc to ignore node_modules, you need a two-pronged approach in your tsconfig.json. First, add "node_modules" to the "exclude" array at the root level of your configuration. However, "exclude" only prevents the compiler from finding files as part of its initial search; if a file in your project imports a library, TypeScript will still look into node_modules. To stop it from type-checking those external declaration files, you must also set "skipLibCheck": true inside "compilerOptions". This is the single most effective way to boost compilation speed and suppress errors stemming from your dependencies' internal types, as it instructs the compiler to skip the full check of all .d.ts files

0
GR
Answered on 20-03-2025

If I use the "include" property in my config to specify only my src folder, do I still need to explicitly list node_modules in the "exclude" array? I've read conflicting reports on whether "include" acts as an implicit exclude for everything else, and I'm trying to keep my configuration file as clean as possible.

PA 21-03-2025

Gregory, you’ve hit on a common point of confusion. While using "include": ["src/**/*"] tells the compiler where to start looking, the node_modules folder is so pervasive that many people still add it to "exclude" as a safety measure. The real "magic" happens with skipLibCheck. Without it, if your code in src imports anything from an external package, TypeScript will still dive into node_modules to resolve those types. So, strictly speaking, while an explicit exclude is good practice, skipLibCheck is what actually stops the compiler from being bogged down by your dependencies' type errors.

0
MA
Answered on 25-03-2025

The best fix is setting skipLibCheck: true. This ignores all type errors in the .d.ts files inside node_modules, which is usually where the bulk of build time is wasted.

RA 26-03-2025

I agree with Mark. I’ve seen build times drop by over 50% just by enabling that one flag. It’s a standard setting for almost every professional React or Angular project these days because nobody has time to fix type bugs in a library they didn't write.

Share your thoughts

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

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session