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
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
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.
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.
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.
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.