Skip to content
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

Webpack Alias #2188

Closed
khankuan opened this issue May 17, 2017 · 17 comments
Closed

Webpack Alias #2188

khankuan opened this issue May 17, 2017 · 17 comments

Comments

@khankuan
Copy link

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!

@gaearon
Copy link
Contributor

gaearon commented May 17, 2017

You can put

NODE_PATH=src

in your .env file for this.

Hope it helps!

@abdennour
Copy link

@gaearon Thanks ! It works like a charm . I just to want to clarify to community how to import files after adding NODE_PATH=src in .env file.

Assuming you want to import src/components/Post.js inside src/containers/Dashboard.js :

Instead of

import Post from  '../components/Post' 
class Dashboard  //... blahblahbla

It will be :

import Post from  'components/Post' // 👈🏼 <----
class Dashboard  //... blahblahbla

@ashleyphillips-dev2rights

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
Copy link

SujitGadkari commented 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:

alias: {
  components: path.join('src/shared/components/'),
  utils: path.join('src/shared/utilities/')
}

which makes imports much cleaner, for example:

import Chart from 'components/Chart'; 

@jochienabuurs
Copy link

Would be nice if this is supported.

@danyim
Copy link

danyim commented Oct 18, 2017

@SujitGadkari Does this actually work with CRA? I haven't been able to get it working.

@SujitGadkari
Copy link

@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
Copy link

Right, It's time to eject it.

  • npm run eject
  • Add the following code in webpack.config.[dev|prod].js
 alias: {
+     '@': path.resolve(__dirname, '../src'),
       // ...
 },

And you can do this:

import Chart from '@/components/Chart';

@ashleyphillips-dev2rights
Copy link

ashleyphillips-dev2rights commented 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
Copy link

danyim commented Oct 20, 2017

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?

@michalv8
Copy link

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.

@thangchung
Copy link

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
Copy link

jeffersoneagley commented 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
Copy link

I agree, this would be a great addition

@treyhuffine
Copy link

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
Copy link

I have ejected the application and configured the resolve.alias without success @gaearon

@Timer
Copy link
Contributor

Timer commented 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.

@facebook facebook locked and limited conversation to collaborators Dec 14, 2017
radiator42 referenced this issue in radiator42/app-todos Nov 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests