-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Aliased imports are not supported anymore when creating a new typescript react app #12047
Comments
I have the same issue, trying to resolve the path with alias and doing the webpack config have seen in multiply sites with tsconfig-paths-webpack-plugin and still not working.. |
I have the same issue. |
It's 2022 year. React 18 released, CRA v5 released, nodejs imports option exists, but alias from box not supported =) |
remove all
|
Using Craco could be a short-term solution, but why do they not work out of the box anymore? Weird stuff |
I had to add react-app-rewired and react-app-rewired-alias My files
tsconfig.paths.json {
"compilerOptions": {
"baseUrl": ".",
"paths": {
"routers/*": ["./src/routers/*"]
}
}
} tsocnfig.json {
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"experimentalDecorators": true
},
"include": [
"src"
],
"extends": "./tsconfig.paths.json" <<<<< import alias paths
} config-overrides.js const {alias, configPaths} = require('react-app-rewire-alias')
module.exports = function override(config) {
alias(configPaths())(config)
return config;
} package.json {
...
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
},
...
} |
@charleston10 |
Пичалька |
This is nuts... |
Today have any solution for React 18.2? Because when I use .babelrc and import from tsconfig.js and my error is:
I use CRA and Node 18.12 |
I was able to get aliases working with
devDeps:
The thing I discovered was that the aliases functionality seems to be limited to folder paths only not file aliases unfortunately. in
tsconfig.paths.json
then in the webpack override file
I tried many other options and combinations of alias packages and this is the only way I've been able to get the alias functionality to work with CRA(rewired). Repo with the working code is here: |
Can we get some attention on this? This feels like something that should work out of the box.. |
My solution
|
In 2023 I still cannot get this to work. React 18.2. TS 4.9.4. The "@Aliases" will not work unless I intentionally break the TS config file by adding "extends: false" to the top of the tsconfig file. But I lose all TS error reporting obviously (still get some warnings). None of the suggestions in this long thread solves this issue. Craco, Rewire, everything fails to work. I spent weeks on this issue with zero results. I used cra to create a new app as well, so there's no old code messing things up. This needs some serious attention. |
Although the path alias is still not supported now, but the official documentation has the solution for absolute-import that import modules by tsconfig/jsconfig with options |
Not sure how I missed this, but I just tried what you posted here and it's the only thing that worked. Thanks! |
Why does this not just work... Next js allows it on create app. Just gives you the option to set up @/ as alias so much cleaner .... I shouldn't have to mess with webpack for such a common request. Someone in recta dev team add this!!!! |
With the solution to use react-app-rewired, in package.json I had to change the build directive too, otherwise the override wasn't called: With This is really a shame. I'm gonna skip aliases entirely and use relative pathing until this is resolved: as noted by others, we shouldn't need to use other packages if this is supposed to be supported out of the box. Thanks for everything we do have in this wonderful FOSS though!! |
This question can be closed. The answer how to use Aliased is in the documents. How typescript is included in the new version of React Native. How to use Aliased These are from the officials docs and works NOTE: when making changes in the babel.config file you need to clear the cache before everything works
|
Testing with |
With:
My
And
Import like this is working very well:
Check your |
2023 and we still have this issue.. Time to switch to next.js |
2023 is now ... 🤣 |
Time to switch to vite =)) |
Any movement? Thanks! |
Why does this not just work? i spent so much time on this 😠 |
Next year is coming soon ...🤣 |
End 2023! Still not supported |
This should be an out of the box thing, we need it to be supported. |
Just use Vite guys, it is easy to set up, is a lot faster, and absolute imports work out of the box. |
it's 2024 and this is still not supported, I'm dying |
The |
2024 and you're the only solution that works for me, Thanks!! |
It works! |
you are a lifesaver man. this worked for me. |
2024 is now ... 🤣 |
I created a new React-typescript app via this command with
react@17.0.2
andtypescript@4.5.2
:npx create-react-app my-app --template typescript
Then I decided to define the alias path as I did many times before. I created the
tsconfig.paths.json
in the root of my app.Then I added the
extend
property to thetsconfig.json
file:Also, I installed
react-app-rewired@2.1.8
and created theconfig-override.js
:So I reopened my IDE (VSCode) and ran
npm start
. I must mention I changed scripts in thepackage.json
before running the start command.After running the start command, this message was displayed in a terminal, and compiling is failed:
The following changes are being made to your tsconfig.json file:
-compilerOptions.paths must not be set (aliased imports are not supported)
*Failed to compile.
./src/App.tsx
Module not found: Can't resolve 'components' in .......*
So I started to search about this issue to resolve it. I found many approaches that I will mention some of them below:
1. Go to your
jsconfig.json
file add the base URL to be "."Then you can directly import stuff from the src directory
Result ==> DID NOT WORK!
2. This problem is solved by an alias for rewire. Install
react-app-rewire-alias
then createconfig-override.js
file:Result ==> DID NOT WORK!
3. Using
craco@6.4.1
packageInstall craco and craco-alias
npm install @craco/craco --save
andnpm i -D craco-alias
Create
tsconfig.paths.json
in root directoryExtend
tsconfig.paths.json
intsconfig.json
{
"extends": "./tsconfig.paths.json",
//default configs...
}
Create
craco.config.js
in the root directoryin
package.json
swap"start": "react-scripts start"
with"start": "craco start"
Result ==> DID NOT WORK!
I'm confused because I used the alias path many times before, but it does not work now. I don't want to
eject
my app but using the alias path is helpful.This is my question in the Stackoverflow community.
The text was updated successfully, but these errors were encountered: