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

Solving the ../../../ problem. #3523

Closed
Palisand opened this issue Nov 30, 2017 · 6 comments
Closed

Solving the ../../../ problem. #3523

Palisand opened this issue Nov 30, 2017 · 6 comments

Comments

@Palisand
Copy link

Palisand commented Nov 30, 2017

Is it possible to support importing local modules relative to the root directory (or something along those lines) without ejecting? In other words, go from this:

import bar from '../../../foo/bar';

to this:

import bar from 'foo/bar'; // or '@foo/bar', or '@bar', or what have you

I have done this before with create-react-native-app after ejecting by using the babel-plugin-module-resolver.

This can also be accomplished using Webpack aliases.

@serranoarevalo
Copy link

serranoarevalo commented Nov 30, 2017

Well, you can create a .env file on the root of your project and make the NODE_PATH point to the src folder (or wherever you have your code) all you'll have to do is write:

NODE_PATH=src

CRA will read your .env file without ejecting.

@Palisand
Copy link
Author

Palisand commented Nov 30, 2017

@Timer Was this closed because using NODE_PATH, which some would consider confusing or an anti-pattern, is really the only way to accomplish this?

@Timer
Copy link
Contributor

Timer commented Nov 30, 2017

Yes, NODE_PATH is the only way without ejecting.
If you'd like more context/information on this topic, feel free to search for previous issues.

image

@Palisand
Copy link
Author

@Timer I had already searched through the issues! ... with the wrong query. I'll be damned: #2188

@percyhanna
Copy link

NODE_PATH doesn't seem like the right solution, as it's very sensitive to local environments.

Is there a reason why paths.appSrc isn't added to resolve as a default source path? That seems like a much better solution that provides absolute paths out of the box, and isn't fragile based on user environment/configuration.

@jangxyz
Copy link

jangxyz commented Mar 24, 2018

Until CRA supports providing alias, I'm going with with adding an symlink inside node_modules/.
I'm fairly fine with adding a single alias @: src, so I ended up adding this to packages.json:

"postinstall": "ln -sf ../src node_modules/@"

@facebook facebook locked as resolved and limited conversation to collaborators Mar 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants