typescripttsconfigtsconfig-paths

Setting tsconfig.json target breaks paths


Here's my import

import { test } from "test";

These settings will import correctly, but are missing modern TS features

//tsconfig.json
{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "test": ["../test"]
        }
    },
}

These settings don't import at all

//tsconfig.json
{
    "target": "ESNext",  // added a target
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "test": ["../test"]
        }
    },
}

If I test through, it appears paths work as expected for ES5, and ES6 and beyond they do not work, nor give any meaningful errors.


What's going on? Why don't paths work the same in newer targets? What has changed, and what am I supposed to do about it?


Solution

  • "moduleResolution": "node" must be added for paths to work in ES6 and above

    //tsconfig.json
    {
        "target": "ESNext",  // added a target
        "compilerOptions": {
            "baseUrl": ".",
            "moduleResolution": "node"
            "paths": {
                "test": ["../test"]
            }
        },
    }
    

    If you don't include a target, it will be ES5 by default.