question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add baseUrl and paths in tsconfig.json and jsconfig.json

See original GitHub issue

tsconfig.json or jsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*":["src/*"]
    }
  }
}

see https://github.com/facebook/create-react-app/issues/5118

This will help VSCode and other IDE to resolve path in TypeScript & JavaScript.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:163
  • Comments:89 (27 by maintainers)

github_iconTop GitHub Comments

139reactions
babaknesscommented, Nov 1, 2018

As a hack/work around, one can set the desired configurations in a different .json file then use the extends features of tsconfig.json. While you do get a warning, it does not override and the settings and proceeds. Using this setting I can test and build without issue (so far)

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "svg/*": ["src/svg/*"],
      "components/*": ["src/components/*"]
    }
  }
}

in tsconfig.json I have

{
  "extends": "./paths.json"
}
106reactions
mgcreacommented, Mar 12, 2019

My 2cts after having played with this for a while:

For a solution to absolute paths without any customization, you can use the following solution (tested with react-scripts@2.1.3, react-scripts@2.1.8).

  1. Add a .env file:
NODE_PATH=./
  1. Add a tsconfig.paths.json file:
{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "src/*": ["*"]
    }
  }
}
  1. Add the following line at the end of the generated tsconfig.json.
"extends": "./tsconfig.paths.json"

Both compilation and intellisense should now work with absolute import support using the src/ prefix 🎉 (eg. import Counter from 'src/components/Counter';)

You can even reuse the exact same setup (without even needing the extends hack) for non-typescript projects with a jsconfig.json file.

I quite like the src/ prefix convention which is perfectly explicit (Principle of Least Surprise) vs a more cryptic @/ or ~/, tilde is also commonly aliased tonode_modules/ in the scss world so I think it should be avoided.

And the bonus point for src/ is that it easily works outside the box thanks to the NODE_PATH env var without any extra webpack tweaks. Also works with jest tests (but if you want to store them inside the root test directory, you’ll have to add "test" to the "include" array in the tsconfig.json (won’t be overwritten).

Finally if you start working on mono-repos of CRA’s (I’ve been doing that lately to leverage a shared storybook-based components “library” across multiple apps), you’ll encounter errors when cross referencing files that all uses the same absolute convention, I’ve built a babel plugin to address that, might be useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use paths in tsconfig.json?
Just specify the baseUrl, config will take care of your relative path. way to config: tsconfig.json file add the below properties.
Read more >
jsconfig.json Reference - Visual Studio Code
The jsconfig. json file specifies the root files and the options for the features provided by the JavaScript language service.
Read more >
TSConfig Reference - Docs on every TSConfig option
Specifies an array of filenames or patterns to include in the program. These filenames are resolved relative to the directory containing the tsconfig.json...
Read more >
tsconfig-paths
Use this to load modules whose location is specified in the paths section of tsconfig.json or jsconfig.json . Both loading at run-time and ......
Read more >
Paths and baseUrl in tsconfig.json
Which refers to the directory that you need your application to consider as a root directry or a base url, it's value might...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found