Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsc doesn't resolve module specified in the typeRoots #13581

Closed
maxkoretskyi opened this issue Jan 19, 2017 · 6 comments
Closed

tsc doesn't resolve module specified in the typeRoots #13581

maxkoretskyi opened this issue Jan 19, 2017 · 6 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@maxkoretskyi
Copy link

maxkoretskyi commented Jan 19, 2017

TypeScript Version: 2.1.5

I have the following directory structure:

tsconfig.json
a.ts
node_modules
|--- custom
     |--- rembo
           |---index.d.ts

The content of index.d.ts is the following:

export declare class Rembo {
    name: string;
}

I reference this module in a.ts:
import {Rembo} from 'rembo';

The package.json is the following:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es6",
    "target": "es6",
    "typeRoots": [
      "node_modules/custom"
    ]
  }
}

The problem is that rembo module can't be resolved. When compiled, I get the following result:

$ tsc --traceResolution

======== Resolving type reference directive 'rembo', containing file 'D:/Projects/typescript/__inferred type names__.ts', root directory 'D:/Projects/typescript/node_modules/custom'. ========
Resolving with primary search path 'D:/Projects/typescript/node_modules/custom'
File 'D:/Projects/typescript/node_modules/custom/rembo/package.json' does not exist.
File 'D:/Projects/typescript/node_modules/custom/rembo/index.d.ts' exist - use it as a name resolution result.
Resolving real path for 'D:/Projects/typescript/node_modules/custom/rembo/index.d.ts', result 'D:/Projects/typescript/node_modules/custom/rembo/index.d.ts'
======== Type reference directive 'rembo' was successfully resolved to 'D:/Projects/typescript/node_modules/custom/rembo/index.d.ts', primary: true. ========
a.ts(3,21): error TS2307: Cannot find module 'rembo'.

I don't understand why the error, since it reports then:

Type reference directive 'rembo' was successfully resolved to 'D:/Projects/typescript/node_modules/custom/rembo/index.d.ts', primary: true.

@mhegazy
Copy link
Contributor

mhegazy commented Jan 19, 2017

TypeRoots is useful for global declarations, things that you do not import, e.g. node definition file. when the compiler starts it loads all files under typeRoots and adds them in the global scope.

Seems like this is not what you are looking for, what you need is Path mapping:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es6",
    "target": "es6",
    "baseUrl": "./",
    "paths": {
      "*" : ["node_modules/custom/*"]
    }
  }
}

This tells the compiler to look under <baseUrl>/node_modules/custom for any import it sees.

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Jan 19, 2017
@maxkoretskyi
Copy link
Author

maxkoretskyi commented Jan 20, 2017

Thanks a lot for you quick reply. As I understand, the node_modules and node_modules/@types are automatically added to "paths", correct? I have imports of one module residing inside node_modules and the other in node_modules/@types and don't "paths" configured, but both modules are still successfully resolved.

And is my understanding correct that both "typeRoots" and "types" is used for global modules resolve?

@mhegazy
Copy link
Contributor

mhegazy commented Jan 21, 2017

that is correct. I would not think about typeRoots unless you are using something global, e.g. node. otherwise, just think about modules.

@maxkoretskyi
Copy link
Author

Got it, thanks. Where can I read about all supported patterns for paths? For example, can I match part of the module name and provide mappings for that part? Or only * pattern is supported?

@gdamjan
Copy link

gdamjan commented Feb 10, 2017

What if the node_modules including node_modules/@types are outside the source directory alltogether?

SRC=$HOME/Pojects/demo
DEST=/tmp/build

ln -s $SRC/package.json $DEST/package.json
cd $DEST
npm install

cd $SRC
tsc --outDir $DEST/build --typeRoots $DEST/???

this doesn't work

(ts 2.1.6)

@mhegazy
Copy link
Contributor

mhegazy commented Feb 10, 2017

paths is relative to the baseUrl and not the projectRoot. so you can make the baseUrl whatever you want.

@mhegazy mhegazy closed this as completed Apr 21, 2017
Hotell added a commit to Hotell/react-tools-for-better-angular-apps that referenced this issue Mar 26, 2018
Hotell added a commit to Hotell/react-tools-for-better-angular-apps that referenced this issue Mar 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants