Resolving typescript directory aliases to relative paths

Viewed 1327

I'm just starting up with typescript and trying to get module resolution working. My issue is pretty simple (I think), but I'm not sure what i'm doing wrong with my tsconfig.json.

If I have a folder structure of:

+ release
    + definitions
    + js
        > a.js
        > main.js
+ src
    > main.ts
    > a.ts

Using gulp, I'm doing incremental compilation like in the gulp example.

And my tsconfig.json is:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": true,
        "moduleResolution": "node",
        "sourceMap": true,
        "outDir": "dist",
        "baseUrl": ".",
        "paths": {
           "src/*": [
               "./src/*"
           ]
        }
    },
    "files": [
        "src/main.ts"
    ],
    "include": [
        "src/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

I'm trying to allow for aliasing of the src directory as src/module_name so resolutions work correctly for stuff like import foo from 'src/la/ja'. So if I'm in a deeply nested file, such as src/something/other/foo.ts and I want to import something from a utils folder in src, I can avoid messy paths import bar from '../../../utils/bar' and just do from 'src/utils/bar'.

Inside my main.ts, i'm trying to get:

import { a } from 'src/a'

To compile with the correct relative directory. With my current tsconfig.json, this resolves (definitions seem fine) and doesn't throw any linting errors, but when it compiles - it compiles as:

const a_1 = require("src/a");

instead of

const a_1 = require("./a");

So when trying to run it, it fails since src/ is not found in release/js/. Is there any way to force typescript to compile to relative paths?

1 Answers
Related