Typescript: Imports in index.ts file are not getting correctly resolved.

Created on 27 Dec 2018  路  16Comments  路  Source: microsoft/TypeScript

"typescript": "3.2.2"

Imports in index.ts file are not getting correctly imported.
Inside engine folder, I have files as

  • engine

    • index.ts

    • engine.ts

    • test.ts

Inside index.ts,
I have

export { Engine } from './engine';
export { Test } from './test';

When I try to import, Engine class in another file using
import {Engine} from '../engine';
engine is not defined

I get it correct if I write this way.
import {Engine} from '../engine/engine';
This was working fine previously, but after resent ts version upgrade it is giving this error.

Question

Most helpful comment

try setting "moduleResolution": "node" in compilerOptions of your tsconfig.json

All 16 comments

try setting "moduleResolution": "node" in compilerOptions of your tsconfig.json

@ajafff It is already set

"compilerOptions": {
"allowJs": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"isolatedModules": false,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
//"declaration": false,
"noImplicitAny": false,
"removeComments": false,
"noLib": false,
"preserveConstEnums": true,
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"suppressImplicitAnyIndexErrors": true,
"sourceMap": true,
"outDir": "release/",
"rootDir": "./",
"watch": true
},

You can see whats really happening by passing --traceResolution option

@sheetalkamat
Got this msg:
What does it mean?

======== Module name '../engine' was successfully resolved to '/Users/sanketd/Documents/Proj/helloworld/app/engine/index.ts'. ========

======== Module name './engine' was successfully resolved to '/Users/sanketd/Documents/Proj/helloworld/app/engine/engine.ts'. ========

The full log would be more informative. It would tell you why ../engine was resolved to the file you are seeing.

Presuming that your folder structure looks something like this:

engine
    \index.ts
    \engine.ts
    \test.ts
other-file.ts

Then shouldn't other-file.ts be doing:

import { Engine } from './engine';

./ means "the directory that contains this module"
../ means "the parent directory of the one that contains this module".

@fatcerberus
Directory is like this.

engine
index.ts
\engine.ts
\test.ts
routes
\tracker.ts

Inside tracker , I am doing,
import { Engine as BotEngine } from '../engine';
Which is shown correct path in vscode as well.

Hmm... it seems a bit questionable from a structural point of view to import from the index.ts at your own level by way of the parent directory, but yeah, I don鈥檛 see any reason that shouldn鈥檛 work.

 - engine
    -  \index.ts
    -  \engine.ts
    -  \test.ts
 -  routes
    -  \tracker.ts
 -  server.ts

In server.ts, I changed,
import {Engine} from './engine/engine';
to
import {Engine} from './engine';

Maybe engine/engine was giving the issue as index.ts already had engine exported.
And it started working. I don't know why this wasn't an issue before.

I have also run into this problem when incorporating unit testing into our project.
I'll just use the given example to expand a bit on it.

Usually everything outside the 'engine' folder uses
import {Engine} from './engine'

Since the test files live inside an 'engine/test' folder in our project, we used
import {Engine} from '../engine'
instead of
import {Engine} from '../../engine' or import {Engine} from '../'

When the tests are run, the first import of _Engine_ is done via
import {Engine} from '../engine'
in the test file.

This causes the
export * from './engine'
inside _index.ts_ to fail.

When running the project normally, the calling order is reversed as all external files import via _index.ts_ so everything works as expected.
It can be fixed by importing through _index.ts_ in the tests, but isn't this something TypeScript should solve on a compiler level?
I don't think importing the way we originally did is a counterintuitive approach and it leads to quite hard to debug issues.

To help understanding, this is the directory structure as a whole:

 - engine
    -  \index.ts
    -  \engine.ts
    -  \test
        - \test.spec.ts
 -  server.ts

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Any recommendations available for writing exports in index.ts and using the exported modules internnally ? ..
this issue is really frustrating to debug : (..

Was this page helpful?
0 / 5 - 0 ratings