Weston
Weston

Reputation: 1451

Setting up absolute module resolution in Typescript and Node

Say I just have my folder structure like this

my_project \
    dist \
        index.js
        logger.js
    src \
        index.ts
        logger.ts
    tsconfig.json

and in index.ts I want to do something like this

import logger from 'src/logger';

how would I do it? I tried adding

...
basedir: ".",
paths: ["src"]
...

to tsconfig. This got it to compile and find the logger module at compile time, but then when I build and everything gets compiled to dist, index.js has require('src/logger'). Obviously Node has no idea what src is so this doesn't work. So how could I get this set up so that the built modules have the correct import paths?

Upvotes: 0

Views: 116

Answers (1)

Burt_Harris
Burt_Harris

Reputation: 6874

I think what you're really looking for is relative paths, not absolute. Have you tried using

import logger from './logger';

Upvotes: 2

Related Questions