0

I currently have little more than the shell of a Typescript & nodejs project repo in Visual Studio, and I can't figure out an import error. I have a top-level file, application.ts, that is erroring when trying to import a local folder, source, which has an index.ts file. My directory structure looks like this:

package.json
tsconfig.json
application.ts
source
    index.ts

And, the error in appliation.js is Cannot find module './source'.

enter image description here

enter image description here

For the record, I also tried import { } from 'source', dropping the ./ but that also did not work. Am I missing something really obvious here?

EDIT: Adding contents of tsconfig.json and package.json for relevancy.

enter image description here

enter image description here

16
  • @RokoC.Buljan I was under the impression that folders can be imported in this fashion if they have an index.ts file. If there is another way to import a folder, please share.
    – Anthony
    Mar 1, 2020 at 22:19
  • 1
    @roko that doesn't make much sense given that every module on npm does exactly this. Mar 1, 2020 at 22:24
  • 2
    Does actually compiling it work? Or does that fail too? Mar 1, 2020 at 22:25
  • 1
    @MartinWahlberg I googled how to restart TS-server in VS, but I couldn't find it. Mind sharing how to do that?
    – Anthony
    Mar 1, 2020 at 22:28
  • 1
    @JonasWilms the compilation fails, and the failure mentions the error above (TS) Cannot find module './source'.
    – Anthony
    Mar 1, 2020 at 22:29

1 Answer 1

1

I think if you add "include": ["./**/*"] to your tsconfig.json it will solve the problem

Make it look something like this:

Keep what you have in compilerOptions and exclude but add include in between like this

"compilerOptions":{},
"include":["./**/*"],
"exclude":[]
2
  • 1
    This worked! Turns out having "target": "ESNext" or "module": "ESNext" caused the error. I guess I misunderstood what they mean or my knowledge is out-of-date. Thank you.
    – Anthony
    Mar 1, 2020 at 22:49
  • 1
    I should clarify now that, after working with this for a minute, "moduleResolution": "Node" was needed to use the index.ts file. That was the root of the issue.
    – Anthony
    Mar 1, 2020 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.