TypeScript: Exclude option is not respected in tsc

TypeScript Version: 1.8.7 1.8.0 1.7.5

Code

{
  "exclude": ["node_modules"]
}

Expected behavior: Shouldn’t build any .ts files in node_modules.

Actual behavior: Builds files in node_modules. If target: es6 I get lots of SyntaxErrors in node.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 4
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I see your point, and I understand the reason. Still I believe something should be done.

So this becomes really inconvenient with node_modules written in typescript. I don’t want typescript touching my node_modules folder. Generating .d.ts files doesn’t help either. Isn’t there a way to avoid this? Or should I not publish source files to npm?

One thing to note, exclude is about the compiler automatically loading all files from your folder when you run tsc with no file list. This does not impact in any shape of form how import statements are resolved. if the compiler sees import * from "mod", and --moduleResolution node then it will try to find mod in node_modules and if it found it it will include it.

if this is not what you want, use --moduleResolution classic.

I’ve seen this be problematic when you are using npm link. When npm link is used you will see both d.ts and .ts files in the node_modules folder for your particular dependency. Then the compiler will try and resolve the imports (and compile them) which would overwrite the d.ts files, which are input files.

I’ve been able to get around this by doing an npm pack (which honors the .npmignore file) and then extracting, and then linking the extracted package. Kind of a pain, but it works I guess.

if you exclude a folder (e.g. node_modules) it will not be included in the compilation. However, if you have an import (e.g. import .. from "@angular\core") it is going to load the required file from the excluded folder. So i guess it is all about the errors you are getting. what are they…

As I mentioned in https://github.com/Microsoft/TypeScript/issues/7432#issuecomment-193891886, and as mentioned in https://github.com/Microsoft/TypeScript/wiki/tsconfig.json:

Any files that are referenced by those specified in the “files” property are also included. Similarly, if a file B.ts is referenced by another file A.ts, then B.ts cannot be excluded unless the referencing file A.ts is also specified in the “exclude” list.

exclude is just a convenience. it saves you from writing the names of all your files on the command line tsc a.ts b.ts c.ts ....

Now you have an import './b', this might have a side effect, i.e. a module augmentation (see https://github.com/Microsoft/TypeScript/wiki/What’s-new-in-TypeScript#augmenting-globalmodule-scope-from-modules) and that needs to be visible in your system. moreover, if you use any imported values e.g. import {a} from 'b', the compiler needs to know what a means to be able to give you any meanigful type checking.