3

I am going nuts currently why my typescript compilation (tsc) is always trying to compile node_modules files even when I've specified to exclude this folder.

[tl;dr; : it's because I have imports but I don't know how to exclude imports from compilation]

I'm using Visual Studio Code, but have the same issue when running tsc directly from commandline

My project structure is a typical one :

.
|
--node_modules\..
|
--src\..
|
--typings

In my root I have a tsconfig.json with following content:

{
 "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "noImplicitAny": false,
    "sourceMap": false,
    "watch": false
 },
 "compileOnSave": true,
 "exclude":
 [
  "node_modules"
 ]
}

But when I compile (using tsc directly in commandline or through visual studio code) I see a zillion of errors from \node_modules\angular..

Now, I do use import statements in my typescript files (because I want to use webpack in the end):

import * as angular from 'angular';
import router from 'angular-ui-router';

So, it seems the typescript compiler tries to import these angular modules AND compile...

I tried to use the --noResolve option in the tsconfig.json, but then my source files throw errors that they cannot find these modules..

I there any way I can tell the typescript compiler to not compile imported modules from folder xyz ?

AardVark71
  • 3,928
  • 2
  • 30
  • 50
  • You can check the version of TS you are using, perhaps it does not support `exclude`? – Ilya Chernomordik Nov 11 '16 at 12:32
  • @IlyaChernomordik Yes, forgot to mention, I did that. I originally had 1.5.8, then upgraded to latest and now have 2.0.9 – AardVark71 Nov 11 '16 at 13:30
  • 1
    To be honest, I never got this "exclude" working and used "include" instead :) That is usually a better alternative if you can manage that (not always unfortunately) – Ilya Chernomordik Nov 11 '16 at 13:32
  • @IlyaChernomordik yes, tried that too. _"include": ["src"]_ but that also pulls the angular modules in (through the import I guess).. even tried to move my tsconfig.json to the src folder, goes bad too – AardVark71 Nov 11 '16 at 13:35
  • well I suppose it needs the `.d.ts` anyway, so you won't get rid of that. But it should not compile the `.js` files, are you sure that js gets compiled? – Ilya Chernomordik Nov 11 '16 at 13:45
  • @IlyaChernomordik your reference to use "include" instead prompted me to look further.. finally made it working with the new --slipLibCheck flag and _not_ specifying either include nor exclude (https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#user-content-new---skiplibcheck) – AardVark71 Nov 11 '16 at 14:25
  • 1
    Wow, that's looks very useful. It was so slow doing all that stuff and you can't use strictNullChecks with Angular2 e.g. But what you say means that actually nothing was compiled as I thought. It's only `.d.ts` files – Ilya Chernomordik Nov 11 '16 at 14:26

1 Answers1

1

In the end, my solution was not by adjusting my tsconfig.json file, but to call the compiler with the new --skipLibCheck flag (added in TypeScript 2.0 - see here in the typescript wiki)

>tsc --skipLibCheck

I also removed the exclude property from my tsconfig.json (as the "exclude" property defaults to excluding the node_modules anyway when not specified (see in the typescript docs).

Community
  • 1
  • 1
AardVark71
  • 3,928
  • 2
  • 30
  • 50
  • Thanks, finally something that helped me! – LauroSkr Jun 20 '20 at 07:40
  • 1
    When I do `import passport from 'passport` I am still getting an error for `passport.authenticate.mockReturnValue(user);` in my excluded file where my tsconfig has `"skipLibCheck": true,`. This answer is mentioned here and a couple other SO questions but hasn't helped me. Any idea there @aardvark71? – Dave Stein Jun 30 '21 at 18:44