2

I am relatively new to typescript so maybe I am missing something here or don't fully understand the concept. In tsconfig I have defined the following in compilerOptions:

{
  baseUrl: ".",
  paths: {
    "*": ["server/*"]
  }
}

Assuming the following directory structure:

+ server
|  + routes
|  |  + util
|  |  - config.routes.ts
|  - route_file.ts
-tsconfig.json

If I import a module exported by route_file.ts in config.routes.ts:

import * as routes from 'routes/route_file.ts';

Typescript resolves the path without issue. The compiled JS, however, requires the module with the same literal path:

const routes = require('routes/route_file')

This is obviously a problem because the module will not be found as the path should be ../route_file. My project has quite a few sub directories and I was hoping to get away from ../../../ if possible. Is there a way to make the compiler check the directory of the importing file and update the require path relative to that file?

2
  • Have you tried to import from an absolute path like '/server/route_file'? Commented Dec 23, 2016 at 23:00
  • Hi @Zofskeez, I had the same question and found an answer, maybe this can be of some help : stackoverflow.com/questions/43657948/… Commented May 10, 2017 at 8:01

1 Answer 1

3

The problem here is that the tsconfig only tells the typescript compiler where to find the type information. The resolving of the paths is a task for the bundler like webpack. Or the runtime executing the JavaScript.
If you want to have a look on a working configuration with webpack you can check out a sample with react I set up a few weeks ago https://github.com/kaoDev/react-ts-sample

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.