Add baseUrl and paths in tsconfig.json and jsconfig.json
See original GitHub issuetsconfig.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:
- Created 5 years ago
- Reactions:163
- Comments:89 (27 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a hack/work around, one can set the desired configurations in a different
.json
file then use theextends
features oftsconfig.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)in
tsconfig.json
I haveMy 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
)..env
file:tsconfig.paths.json
file:tsconfig.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 theNODE_PATH
env var without any extra webpack tweaks. Also works with jest tests (but if you want to store them inside the roottest
directory, you’ll have to add"test"
to the"include"
array in thetsconfig.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.