Typescript: "moduleResolution": "node" usage and absolute/relative imports

Created on 17 Oct 2015  路  8Comments  路  Source: microsoft/TypeScript

I have recently ran into some issues after having added "moduleResolution": "node" to my tsconfig.json:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "module": "system",
        "moduleResolution": "node",
        "noImplicitAny": false,
        "outDir": "dist",
        "rootDir": "./src",
        "sourceMap": true,
        "target": "es5"
    },
    "exclude": [
        "node_modules"
    ]
}

The folder structure of the project looks like:

src/
    app/
        components/
            app/
                app.ts
                app.html
                app.css
        components.ts
        ...
    common/
        animation.ts
        ...

The problem is that when I import { Animation } from 'common/animation'; it tells me that it cannot find the module, so I have to use import { Animation } from '../../../common/animation';. Is it really necessary to have the path as such? And why does it not try to resolve the common/animation relative to rootDir?

Question

All 8 comments

node module resolution mimics the way how node looks for modules in runtime (resolution algorithm can be found here). In your case name is not relative - module will always be searched as node module using folder walk and lookup inside node_modules folder. In your case you can specify moduleResolution as classic to use pre-TypeScript 1.6 module resolution logic. Also we have a proposal for path mappings based resolution which I think should do exactly what you need.

Out of curiosity, why do you want to use node module resolution in the first place?

I see, makes sense. It is because of Angular 2, it looks like the type definitions work better if I use this option, whereas every time I use the classic resolution I get into trouble with some typings. But it looks like 5039 will solve my issue. Is there a eta on that? Or is it available on the nightly builds?

we plan to start working on this issue quite soon

That sounds great. Looking forward :+1:

Any news here? With angular2 success i think will be really important to resolve modules starting to rootDir with node moduleResolution setting.

Same here - we are doing really fancy stuff and need more flexibility regarding import paths. Your suggestion regarding paths similar to systemjs import settings in 5039 would help a lot!

Same problem. Any solution?

please see the documentation in #5039, and the implementation should be in typescript@next.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

blendsdk picture blendsdk  路  3Comments

siddjain picture siddjain  路  3Comments

Roam-Cooper picture Roam-Cooper  路  3Comments

uber5001 picture uber5001  路  3Comments