Using the tsconfig file to configure the alias does not take effect
See original GitHub issueThe project was generated using the create-react-app scaffolding, and the paths option was removed when I used the tsconfig.json file to configure the path alias! Then I tried to use the extends property in the tsconfig.json file to connect to the external path configuration file, but it didn’t work either! Using the customize-cra plug-in in conjunction with the above configuration file does not take effect. How do I configure it? Why doesn’t the scaffolding support?
React version: 16.13.1 react-scripts version: 3.4.1
{
"extends": "./tsconfig.paths.json", // Subsequent configuration options
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react"
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
The tsconfig.paths. json path file configuration:
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@/*": ["src/*"],
"actions/*": ["src/actions"],
"api/*": ["src/api"],
"common/*": ["src/common"],
"components/*": ["src/components"],
"containers/*": ["src/containers"],
"helper/*": ["src/helper"],
"icon/*": ["src/icon"],
"reducers/*": ["src/reducers"]
}
}
}
Customize -cra configuration file:
const { override, addDecoratorsLegacy, addWebpackAlias } = require("customize-cra")
const path = require("path")
module.exports = override(
addDecoratorsLegacy(),
addWebpackAlias({
"@": path.resolve(__dirname, 'src'),
"actions": path.resolve(__dirname, 'src/actions'),
"api": path.resolve(__dirname, 'src/api'),
"common": path.resolve(__dirname, 'src/common'),
"components": path.resolve(__dirname, 'src/components'),
"containers": path.resolve(__dirname, 'src/containers'),
"helper": path.resolve(__dirname, 'src/helper'),
"icon": path.resolve(__dirname, 'src/icon'),
"reducers": path.resolve(__dirname, 'src/reducers')
})
)
Sample file contents:
import App from 'components/App'
Warning message:
The following changes are being made to your tsconfig.json file:
- compilerOptions.paths must not be set (aliased imports are not supported)
Error message:
./src/index.tsx
Module not found: Can't resolve 'components/App' in 'D:\code\my-music\src'
Please help me thank you!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:47
- Comments:41
Top Results From Across the Web
compilerOptions.paths must not be set (alias imports are not ...
when I compile my project in VS Code, it removes the paths entry from tsconfig.json with the following message. Why alias imports are...
Read more >How To Configure Path Aliases With TypeScript
In this article, I'll show how to configure path aliasing in a TypeScript project. The example project will be overly simplistic, ...
Read more >Typescript — How to solve the problem with unresolved path ...
js file containing the line with the path alias. The actual cause of the issue is that the tsc compiler does not transpiles...
Read more >tsc-alias - npm
Overwrite file extensions tsc-alias will use to scan and resolve files. undefined. Configuration via tsconfig.json Example. { "compilerOptions": ...
Read more >Typescript – How to solve the problem with unresolved path ...
Setting up path aliases in the tsconfig.json file is really a nice ... we work in a relatively simple Typescript project – with...
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
It is definitely bad to modify user’s custom
tsconfig.json
without options!I found a workaround:
Move current
tsconfig.json
totsconfig.base.json
, and add a new filetsconfig.json
:Is there at least a good reason for not allowing path aliases in CRA?