380

I was reading about path-mapping in tsconfig.json and I wanted to use it to avoid using the following ugly paths:

enter image description here

The project organization is a bit weird because we have a mono-repository that contains projects and libraries. The projects are grouped by company and by browser / server / universal.

enter image description here

How can I configure the paths in tsconfig.json so instead of:

import { Something } from "../../../../../lib/src/[browser/server/universal]/...";

I can use:

import { Something } from "lib/src/[browser/server/universal]/...";

Will something else be required in the webpack config? or is the tsconfig.json enough?

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93

20 Answers20

551

This can be set up on your tsconfig.json file, as it is a TS feature.

You can do like this:

"compilerOptions": {
        "baseUrl": "src", // This must be specified if "paths" is.
         ...
        "paths": {
            "@app/*": ["app/*"],
            "@config/*": ["app/_config/*"],
            "@environment/*": ["environments/*"],
            "@shared/*": ["app/_shared/*"],
            "@helpers/*": ["helpers/*"]
        },
        ...

Have in mind that the path where you want to refer to, it takes your baseUrl as the base of the route you are pointing to and it's mandatory as described on the doc.

The character '@' is not mandatory.

After you set it up on that way, you can easily use it like this:

import { Yo } from '@config/index';

the only thing you might notice is that the intellisense does not work in the current latest version, so I would suggest to follow an index convention for importing/exporting files.

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

Arnold Schrijver
  • 3,588
  • 3
  • 36
  • 65
Alejandro Lora
  • 7,203
  • 3
  • 18
  • 34
  • 101
    Just a comment that might help others... if you are working with node.js or some environment that doesn't use a module bundler like webpack you will additionally need the https://www.npmjs.com/package/module-alias module – Remo H. Jansen Jan 05 '18 at 14:24
  • 3
    @Alejandro Lora I've used this solution in my project it works like charm but when i run ng test karma is not able to resolve environment variables. what could be the reason? – Gavishiddappa Gadagi Apr 25 '18 at 13:27
  • Can you provide an example of this? can't reproduce it locally @GavishiddappaGadagi – Alejandro Lora May 03 '18 at 09:19
  • 1
    This works completely fine but there is a problem when enable declaration and import this npm module in another module. Intelisense breaks. Any idea on how to fix this issue? – Sivasankar May 18 '18 at 14:12
  • @siva - me too. Did you get an answer for this issue? – cubabit Jun 11 '18 at 11:57
  • @cubabit not exactly. We moved to relative paths instead of aliases. But I found few ts plugin which does it: https://github.com/zerkalica/zerollup/tree/master/packages/ts-transform-paths – Sivasankar Jun 12 '18 at 09:20
  • I am using Sublime text 3 and its working fine for me but its breaks ts auto suggestions, how to fix it? – SjVnyk Jun 21 '18 at 09:56
  • you probably can ask that to the sublime team, vscode editor is compatible and works well @SjVnyk – Alejandro Lora Jun 21 '18 at 10:05
  • @AlejandroLora Do you know if it is possible to map a path to a direct file that would export a whole api ? Can't manage to make this work – Aphax Nov 30 '18 at 11:26
  • 2
    I cannot get this to work. I have a tsconfig.json file, and then inside my src I have a tsconfig.app.json file. I have tried adding these values to both, with and without "*" and slashes. I'm just using angular-cli. Is there anything special that has to be done, like for webpack? Thanks! – emery.noel Nov 30 '18 at 21:31
  • 1
    @Aphax yes. it's possible to map to a single file, I prepared an example here: https://github.com/ialex90/TypeScript-Node-Starter/commit/a4e8cc1f8f8d5176e0099e05b51f97b0ef4bebea – Alejandro Lora Dec 03 '18 at 11:26
  • @emery.noel You need to add it only once, add it to the global one tsconfig.json – Alejandro Lora Dec 03 '18 at 11:27
  • 5
    note that jest-test don't use the tsconfig-paths - you need to define a `moduleNameMapper`: [tsjest#414](https://github.com/kulshekhar/ts-jest/issues/414) – TmTron Jan 29 '19 at 19:37
  • this causes issues if you're using Jest BTW as the paths arent resolveable. there's a way to use aliases with jest. But then if you're using node too, it becomes yawnsome to have alias definitions in 4 different places. – Andy Dec 02 '19 at 09:49
  • what if the module specified in the `paths` has its own `package.json` and node_modules? during compilation `node_modules` are not included in the `dist` folder – inside Nov 21 '20 at 17:29
  • 1
    This answer would be much better, if it would have a warning that this will not work out-of-the-box on nodejs. Without such a warning, nodejs user will inevitably run in the [infamous issue](https://github.com/microsoft/TypeScript/issues/10866) that TypeScript will successfully handle the path aliases at compile time, but since it doesn't rewrite import paths, nodejs will fail importing at runtime. – bluenote10 Mar 19 '21 at 11:25
  • Why the hell does no one have the answers to ionic. I guess no one uses it .But I tried about a billion solution similar to this and it doesnt work. I am using ionic 6. everything is just way too outdated – numerical25 Mar 25 '21 at 12:22
  • 3
    in order for the paths to work with nodejs you need to pre-load `tsconfig-paths/register` and `ts-node/register/transpile-only`. – hanan hamza Apr 02 '21 at 09:53
  • im having a really strange issue. In my compiler it works fine on all the includes but as soon as I try and instantiate the class it cant find the module event though in vs code it finds it etc. Any ideas? https://stackoverflow.com/questions/67281799/typescript-aliases-not-working-properly-when-i-init-dependency – TheMan68 Apr 27 '21 at 20:35
  • For me setting the `noEmit` option to `true` in the tsconfig.json solved the problem in a node.js environment. – Vispercept Dec 17 '21 at 09:49
  • While it works for me perfectly with not nested imports, no matter wheter it is with @ and/or initial _, it does not woth nested paths like 'assets/icons' again no matter... – Kiszuriwalilibori Aug 08 '23 at 15:33
43

You can use combination of baseUrl and paths docs.

Assuming root is on the topmost src dir(and I read your image properly) use

// tsconfig.json
{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "lib/*": [
        "src/org/global/lib/*"
      ]
    }
  }
}

For webpack you might also need to add module resolution. For webpack2 this could look like

// webpack.config.js
module.exports = {
    resolve: {
        ...
        modules: [
            ...
            './src/org/global'
        ]
    }
}
mleko
  • 11,650
  • 6
  • 50
  • 71
  • 2
    Just noticing @mleko, @alejandro-lora used `baseUrl`, you talk `rootDir` ... — what's the difference? – Frank N Jan 11 '21 at 10:03
24

Check this similar solutions with asterisk

  "baseUrl": ".",
  "paths": {
    "*": [
      "node_modules/*",
      "src/types/*"
    ]
  },
Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
AgBorkowski
  • 1,021
  • 8
  • 13
17

This works for me:

 yarn add --dev tsconfig-paths

 ts-node -r tsconfig-paths/register <your-index-file>.ts

This loads all paths in tsconfig.json. A sample tsconfig.json:

{
    "compilerOptions": {
        {…}
        "baseUrl": "./src",
        "paths": {
            "assets/*": [ "assets/*" ],
            "styles/*": [ "styles/*" ]
        }
    },
}

Make sure you have both baseUrl and paths for this to work

And then you can import like :

import {AlarmIcon} from 'assets/icons'
FacePalm
  • 10,992
  • 5
  • 48
  • 50
14

If you are using paths, you will need to change back absolute paths to relative paths for it to work after compiling typescript into plain javascript using tsc.

Most popular solution for this has been tsconfig-paths so far.

I've tried it, but it did not work for me for my complicated setup. Also, it resolves paths in run-time, meaning overhead in terms of your package size and resolve performance.

So, I wrote a solution myself, tscpaths.

I'd say it's better overall because it replaces paths at compile-time. It means there is no runtime dependency or any performance overhead. It's pretty simple to use. You just need to add a line to your build scripts in package.json.

The project is pretty young, so there could be some issues if your setup is very complicated. It works flawlessly for my setup, though my setup is fairly complex.

Joon
  • 9,346
  • 8
  • 48
  • 75
14

If you are looking for the most minimalist example for referencing your root folder with @, this would be it:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "@/*": ["*"]
    }
  }
}
// Example usage: import * as logUtils from '@/utils/logUtils';

Or if you don't even have a src folder or would like to explicitly include it in the imports, this would also work:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["*"]
    }
  }
}
// Example usage: import * as logUtils from '@/src/utils/logUtils';
cprcrack
  • 17,118
  • 7
  • 88
  • 91
10

If you are using tsconfig-paths and this does not work for you, try tsconfig.json:

{
  // ...
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "src",
    "baseUrl": ".",
    "paths": {
      "@some-folder/*": ["./src/app/some-folder/*", "./dist/app/some-folder/*"],
      // ...
    }
  },
  // ...
}

If the compiler sees @some-folder/some-class, it is trying to find it in ./src... or in ./dist....

JSideris
  • 5,101
  • 3
  • 32
  • 50
ktretyak
  • 27,251
  • 11
  • 40
  • 63
9

Alejandros answer worked for me, but as I'm using the awesome-typescript-loader with webpack 4, I also had to add the tsconfig-paths-webpack-plugin to my webpack.config file for it to resolve correctly

James Moran
  • 624
  • 1
  • 7
  • 14
6

Its kind of relative path Instead of the below code

import { Something } from "../../../../../lib/src/[browser/server/universal]/...";

We can avoid the "../../../../../" its looking odd and not readable too.

So Typescript config file have answer for the same. Just specify the baseUrl, config will take care of your relative path.

way to config: tsconfig.json file add the below properties.

"baseUrl": "src",
    "paths": {
      "@app/*": [ "app/*" ],
      "@env/*": [ "environments/*" ]
    }

So Finally it will look like below

import { Something } from "@app/src/[browser/server/universal]/...";

Its looks simple,awesome and more readable..

Vijay
  • 61
  • 1
  • 2
6
{
  "compilerOptions": {
    "baseUrl": "src"
  },
  "include": ["src"]
}

i'm not sure if we must define the paths. but after write baseUrl to src i can import all folder under src folder like this

import { Home } from "pages";
import { formatDate } from "utils";
import { Navbar } from "components";

don't forget to restart your terminal after change the tsconfig.json

Zacquio
  • 309
  • 3
  • 2
5

if typescript + webpack 2 + at-loader is being used, there is an additional step (@mleko's solution was only partially working for me):

// tsconfig.json
{
  "compilerOptions": {
    ...
    "rootDir": ".",
    "paths": {
      "lib/*": [
        "src/org/global/lib/*"
      ]
    }
  }
}    

// webpack.config.js
const { TsConfigPathsPlugin } = require('awesome-typescript-loader');

resolve: {
    plugins: [
        new TsConfigPathsPlugin(/* { tsconfig, compiler } */)
    ]
}

Advanced path resolution in TypeScript 2.0

eeglbalazs
  • 1,988
  • 1
  • 14
  • 10
3

Solution for 2021.

Note: CRA. Initially the idea of ​​using a third party library or ejecting app for alias seemed crazy to me. However, after 8 hours of searching (and trying variant with eject), it turned out that this option is the least painful.

Step 1.

yarn add --dev react-app-rewired react-app-rewire-alias

Step 2. Create config-overrides.js file in your project's root and fill it with :

const {alias} = require('react-app-rewire-alias')

module.exports = function override(config) {
  return alias({
    assets: './src/assets',
    '@components': './src/components',
  })(config)
}

Step 3. Fix your package.json file:

  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app-rewired start",
-   "build": "react-scripts build",
+   "build": "react-app-rewired build",
-   "test": "react-scripts test",
+   "test": "react-app-rewired test",
    "eject": "react-scripts eject"
}

If @declarations don't work, add them to the d.ts file. For example: '@constants': './src/constants', => add in react-app-env.d.ts declare module '@constants';

That is all. Now you can continue to use yarn or npm start/build/test commands as usual.

Full version in docs.

Note: The 'Using with ts / js config' part in docs did not work for me. The error "aliased imports are not supported" when building the project remained. So I used an easier way. Luckily it works.

MadaShindeInai
  • 432
  • 4
  • 16
  • 1
    `react-app-rewired` package is [lightly maintained](https://github.com/timarney/react-app-rewired#rewire-your-app-) so you shouldn't continue with this package you should use **craco** instead – Hasan Tezcan Jun 16 '21 at 06:57
  • 1
    I tried installing `craco` today and noticed there were around 20 out of date dependencies, some with critical vulnerabilities. – SimpleProgrammer Dec 28 '21 at 05:31
3

For a component library

If you are working on a library that returns UI components (Like react-bootstrap or antd) then this should work for you.

"compilerOptions": {
        ....
        "rootDir": "src",
        "baseUrl": ".",
        "paths": {
            "src/*": ["src/*"],
            "components/*": ["src/components/*"],
        }
  },
Anjan Talatam
  • 2,212
  • 1
  • 12
  • 26
2

It looks like there has been an update to React that doesn't allow you to set the "paths" in the tsconfig.json anylonger.

Nicely React just outputs a warning:

The following changes are being made to your tsconfig.json file:
  - compilerOptions.paths must not be set (aliased imports are not supported)

then updates your tsconfig.json and removes the entire "paths" section for you. There is a way to get around this run

npm run eject

This will eject all of the create-react-scripts settings by adding config and scripts directories and build/config files into your project. This also allows a lot more control over how everything is built, named etc. by updating the {project}/config/* files.

Then update your tsconfig.json

{
    "compilerOptions": {
        "baseUrl": "./src",
        {…}
        "paths": {
            "assets/*": [ "assets/*" ],
            "styles/*": [ "styles/*" ]
        }
    },
}
Andy Braham
  • 9,594
  • 4
  • 48
  • 56
  • Another workaround for this would be to inherit those values from another file. In your tsconfig, place this param and path to the base file ``` { ... "extends": "./tsconfig.base.json", ... } ``` Then in the `tsconfig.base.json` you can place your paths config ``` { "compilerOptions": { "paths": { "*": ["*"] } } } ``` The error will still show, but it wont delete your settings anymore. There's more info on why this is happening here https://github.com/facebook/create-react-app/issues/10178#issuecomment-860247467 – Lawrence_NT Jan 24 '22 at 23:44
2

Checkout the compiler operation using this

I have added baseUrl in the file for a project like below :

"baseUrl": "src"

It is working fine. So add your base directory for your project.

divya_kanak
  • 142
  • 9
1

/ starts from the root only, to get the relative path we should use ./ or ../

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
  • Not true. Many options allow for [non-relative module imports](https://www.typescriptlang.org/docs/handbook/module-resolution.html). – Nino Filiu Nov 16 '18 at 17:00
1

You can do this with just Node by using Subpath patterns.

For example, adding this to your package.json...

{
    "imports": {
        "#lib": "./build/path/to/lib",
        "#lib/*": "./build/path/to/lib/*",
    }
}

...will let you import like so, avoiding relative paths.

import { something } from "#lib"

Note that they must start with a hash, and in package.json, they must point to your build so Node can recognize it.

Like others have said, you can add something like this to your tsconfig.json for Typescript:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "#lib": ["./src/path/to/lib"],
            "#lib/*": ["./src/path/to/lib/*"],
        },
    },
}
hf02
  • 171
  • 2
  • 11
0

baseUrl being correct is important.

import Home from '@app/features/Home';

src/features/Home.tsx

  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": [
        "src/*"
      ],
    },
Emre
  • 831
  • 11
  • 13
0

Make sure you have:

{
    "compileOnSave": true,
    "compilerOptions": {
        "paths": {
            "@styles/*": ["assets/scss/*"]
        }
    },
}

That was causing me issues.

hf02
  • 171
  • 2
  • 11
Luis Lopez
  • 51
  • 1
  • 1
0

For me, VS Code was not recognizing the path but project was building just fine. The issue was that I had the paths declaration in a second tsconfig.app.json file.

Moving the paths into the primary tsconfig.json file fixed the issue.

Supposedly, this is because VS Code only checks the first tsconfig.json file found in the root directory of the project, although I haven't confirmed that for myself. Perhaps someone can provide a comment with more info.

Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48