You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm wondering if create-react-app supports aliasing. I thought that certain top-level folders can be aliased to prevent having a lot of relative paths. Cheers!
The problem with using NODE_PATH is, if you want to restructure your project, you have to update all your import statements. Whereas if you can use webpacks aliasing, all you have to do is update the alias. Also '@components' is more convenient than 'src/js/components'.
In addition to what @ashleyphillips-dev2rights is saying, how can we handle multiple aliases for shared directories like shared components, directories etc using NODE_PATH ?
For example, with webpack I can have aliases set as:
@danyim No it doesn't. Currently there is no way of providing custom webpack config for CRA unless you eject the app.
One possible solution is to allow users to provide custom config that gets merged with base CRA webpack config before execution, but I believe @gaearon and his team wants to avoid opening up webpack config for CRA's flexibility in choosing/replacing build tool in future. I do still feel that ability to add aliases is useful enough to implement somehow.
@Runrioter Ejecting is exactly what we are trying to avoid. I understand using something like create-react-app (withuot ejecting) will never suit all use cases, but I would consider aliasing a very common thing which shouldn't mean having to eject to be able to do it. Not ejecting has lots of advantages we want to keep so I prefer to not eject where possible, and aliasing is something that most projects can benefit from.
There is already an alias in CRA for react-native-web when the project is ejected. Perhaps having a default @ alias would be beneficial to reduce the number of relative pathing in a project.
resolve: {
...
alias: {
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
'@': paths.appSrc
}
}
Where would this change go if we want to add this behavior to an unejected CRA?
Maybe add create-react-app configuration section in package.json and there we could configure webpack aliases? I know that you want to keep it as simple as it can and without care this section could grow enormously in the future (and could be another big configuration file like webpack.config.js) but some configuration possibility could be very welcome. And alias configuration is rather universal term for any package bundler so it still be independant.
Reacted by Sujit Gadkari, Jochem Nabuurs, Darin Haener, James Greenaway, Michael Senger, Gregor Albrecht, Inventorco, Pavel Chernov, nahum zsilva, David Babel and 7 more
I guess I have to go change all the import statements in my project, or give up on migrating it to CRA. It's probably not worth the time-sink. If I must use eject, I may as well just keep the existing structure. Is there no way to do aliases through babel or something?
I've submitted a PR to use Webpack alias. In the package.json, the user defines an "alias" array which contains objects of the form { "source": "{path}", "expose": "{absolute path}" }.
@gaearon is this something you would be interested in introducing to the code base?
Activity
gaearon commentedon May 17, 2017
You can put
in your
.envfile for this.Hope it helps!
abdennour commentedon Sep 4, 2017
@gaearon Thanks ! It works like a charm . I just to want to clarify to community how to import files after adding
NODE_PATH=srcin.envfile.Assuming you want to import
src/components/Post.jsinsidesrc/containers/Dashboard.js:Instead of
It will be :
ashleyphillips-dev2rights commentedon Sep 21, 2017
The problem with using NODE_PATH is, if you want to restructure your project, you have to update all your import statements. Whereas if you can use webpacks aliasing, all you have to do is update the alias. Also '@components' is more convenient than 'src/js/components'.
SujitGadkari commentedon Oct 3, 2017
In addition to what @ashleyphillips-dev2rights is saying, how can we handle multiple aliases for shared directories like shared components, directories etc using NODE_PATH ?
For example, with webpack I can have aliases set as:
which makes imports much cleaner, for example:
jochienabuurs commentedon Oct 4, 2017
Would be nice if this is supported.
danyim commentedon Oct 18, 2017
@SujitGadkari Does this actually work with CRA? I haven't been able to get it working.
SujitGadkari commentedon Oct 18, 2017
@danyim No it doesn't. Currently there is no way of providing custom webpack config for CRA unless you eject the app.
One possible solution is to allow users to provide custom config that gets merged with base CRA webpack config before execution, but I believe @gaearon and his team wants to avoid opening up webpack config for CRA's flexibility in choosing/replacing build tool in future. I do still feel that ability to add aliases is useful enough to implement somehow.
Runrioter commentedon Oct 18, 2017
Right, It's time to eject it.
webpack.config.[dev|prod].jsalias: { + '@': path.resolve(__dirname, '../src'), // ... },And you can do this:
ashleyphillips-dev2rights commentedon Oct 19, 2017
@Runrioter Ejecting is exactly what we are trying to avoid. I understand using something like create-react-app (withuot ejecting) will never suit all use cases, but I would consider aliasing a very common thing which shouldn't mean having to eject to be able to do it. Not ejecting has lots of advantages we want to keep so I prefer to not eject where possible, and aliasing is something that most projects can benefit from.
danyim commentedon Oct 20, 2017
There is already an alias in CRA for
react-native-webwhen the project is ejected. Perhaps having a default@alias would be beneficial to reduce the number of relative pathing in a project.Where would this change go if we want to add this behavior to an unejected CRA?
michalv8 commentedon Oct 20, 2017
Maybe add
create-react-appconfiguration section inpackage.jsonand there we could configure webpack aliases? I know that you want to keep it as simple as it can and without care this section could grow enormously in the future (and could be another big configuration file likewebpack.config.js) but some configuration possibility could be very welcome. And alias configuration is rather universal term for any package bundler so it still be independant.thangchung commentedon Nov 19, 2017
I struggled to this issue also and found out the article very details (as @gaearon mentioned) at https://medium.com/@ktruong008/absolute-imports-with-create-react-app-4338fbca7e3d
jeffersoneagley commentedon Nov 24, 2017
I guess I have to go change all the import statements in my project, or give up on migrating it to CRA. It's probably not worth the time-sink. If I must use eject, I may as well just keep the existing structure. Is there no way to do aliases through babel or something?
treyhuffine commentedon Dec 2, 2017
I agree, this would be a great addition
treyhuffine commentedon Dec 5, 2017
I've submitted a PR to use Webpack alias. In the package.json, the user defines an
"alias"array which contains objects of the form{ "source": "{path}", "expose": "{absolute path}" }.@gaearon is this something you would be interested in introducing to the code base?
PR #3554
londonbarrett commentedon Dec 14, 2017
I have ejected the application and configured the resolve.alias without success @gaearon
Timer commentedon Dec 14, 2017
We don't provide support once you eject, sorry!
I'm going to lock this issue to prevent further bumps because there's so many active parties. If anyone has issues, please file a new issue.
sagas, form