简体   繁体   中英

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?

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

import logger from './logger';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM