Typescript: Exclude option is not respected in tsc

Created on 8 Mar 2016  路  17Comments  路  Source: microsoft/TypeScript

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.

By Design

Most helpful comment

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?

All 17 comments

The same issue happens when using the "files" option actually. Isn't there a way to void this?

What do your source files and dependencies look like?

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.

Here's the minimal setup:

a.ts

import './b';

b.ts

export = 1;

tsconfig.json

{
  "exclude": ["b.ts"]
}

and then...

$ ls
a.ts b.ts tsconfig.json
$ tsc -p .

$ ls
a.ts a.js b.ts **b.js** tsconfig.json

I would expect tsc not to compile b.ts.

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%27s-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.

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?

At least a way of saying .d.ts files should have higher priority over .ts files

if you do not want the compiler to resolve any files for you, use --noResolve. but that means that all your modules will not be resolved.

I do not think this is the real problem. the real problem is really why you need to exclude some module typings. what is the offending file that you are trying to exclude.

Well, I'm using TS + babel to be able to use async/await in node. So in my tsconfig file I have target: es6 and I cannot change that.
I use babel-register and a small set of transforms with node 5.7. by default babel-register doesn't apply transforms to anything inside node_modules because it will have a huge impact on performance.

I have a couple of npm modules I have written myself in Typescript (one of them is called rabbitr). They are compiled to ES5 and should work fine in node. The problem is when I run tsc -p . it recompiles those modules to ES6, and they suddenly stop working in node unless I whitelist them in babel. I don't want to have to whitelist every single npm module written in typescript. That's the problem.

that is the problem then. the compiler should not compiler your references. this is tracked by https://github.com/Microsoft/TypeScript/issues/6964.

consider using a "typings" property in your package.json for your module (rabbitr). this way you can point it to a .d.ts and that will always be picked. see https://github.com/Microsoft/TypeScript/wiki/Typings-for-npm-packages

Reading this ticket, then it is good to assume to do not exclude the files under node_modules?

I do not understand the question

@mhegazy thank you for having a look to my question.
Looking at this comment https://github.com/Microsoft/TypeScript/issues/7432#issuecomment-193891886, it mentions that excluding node_modules it might have some impact on the project.

The reason of my question is because I have an Angular 2 project using 2.0.1 and when running 'tsc' it runs the transpiling process to all my files, .ts and d.ts under node_modules. Because of this, it throws some errors coming from some libraries (not Angular). I assume because these ones might be using different versions of TypeScript? I wanted to use the exclude option and that brought me here.

What would be the best way to solve this issue? Any help would be greatly appreciated.

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

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.

@mhegazy thank you very much for taking the time to explain!

@landonpoch can you elaborate on the steps needed to npm pack, extract and link.? I am having the problem with an npm linked project.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MartynasZilinskas picture MartynasZilinskas  路  3Comments

kyasbal-1994 picture kyasbal-1994  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

siddjain picture siddjain  路  3Comments