Should the index.ts be resolved by TypeScript as default module file?

Viewed 9033

I am trying to get work the module resolution under typescript.

If I have:

/modulename/index.ts

should it be resolved by:

import * as modulename from "modulename"

?

I can't get it work. But

import * as modulename from "modulename/index"

works well.

Edit

as aluan-haddad recommended me it is neccessary to have the tsc configured properly.

This one worked for me:

{
   ...
   "baseUrl": ".",
   "module": "commonjs",
   "moduleResolution": "node",
   ...
}

Edit

Please note this configuration does not work when used with VS. If it is placed in external tsconfig file the compilation works well but language service can't handle it. If its is placed in msconfig (csporj) both, compiltion and language service fails.

Only the one solution I have found to be working for 100% is to create something like:

src
   node_modules
      module_being_currently_developed
         submodules

In this case the module resolution works properly.

2 Answers

You have to add a slash in the folder (module) name, this will treat index.ts as the folder index.

import * as modulename from "modulename/"
Related