-
-
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
Document existing work-around for absolute imports. #693
Conversation
…sing asbolute paths.
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla - and if you have received this in error or have any questions, please drop us a line at cla@fb.com. Thanks! |
@@ -205,6 +205,34 @@ class Button extends Component { | |||
export default Button; // Don’t forget to use export default! | |||
``` | |||
|
|||
To import your own modules into other files, you can use relative paths by default. For example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would work better as a separate section called “Absolute Imports”. I would put it right before “Can I Use Decorators?” so that it’s there, but doesn’t distract from the usual way of doing things.
```js | ||
import Banana from '../../Banana'; | ||
``` | ||
You can also enable absolute paths by adding a NODE_PATH environment variable. This is a bit of a stop-gap measure for now. Here is an example absolute import and the commands you would need to run for it to work: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s put NODE_PATH
into backticks so that it’s highlighted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let’s expand “This is a bit of a stop-gap measure for now.” into “We don’t recommend this at the moment, and we encourage you to use relative paths for your projects if you can. However, this can be used as a stop-gap measure if you’re porting a large project to Create React App. In the future, we intend to offer a better way of handling absolute imports.”
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
If you use Bash on OS X or Linux: | ||
|
||
```js | ||
NODE_PATH=./src npm start |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let’s recommend doing this inside package.json
scripts
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we do that, we might as well tell people to npm install --save-dev cross-env
and use cross-env NODE_PATH=src react-scripts start
and such so that we don’t need separate instructions for Bash and Cmd.
We should probably also document that we need to configure flow to follow these imports properly:
Where |
@mandysimon88 Thanks for your work on this. Do you think you could find some time to address the review comments? |
Closing as review comments have not been addressed, and we are going with a different strategy for this anyway in #1065. Thanks for your time! |
There's been numerous requests for Create-React-App to support having imports resolved relative to the "src" folder. The semi-documented solution is to have a NODE_PATH environment variable, which will be used in the resolution process. It's apparently also possible to specify that variable in a file named ".env". References: facebook/create-react-app#476 facebook/create-react-app#693 facebook/create-react-app#741
There's been numerous requests for Create-React-App to support having imports resolved relative to the "src" folder. The semi-documented solution is to have a NODE_PATH environment variable, which will be used in the resolution process. It's apparently also possible to specify that variable in a file named ".env". References: facebook/create-react-app#476 facebook/create-react-app#693 facebook/create-react-app#741
Following the suggestion of @gaearon (#476 (comment)).
This documents an existing work-around for people who want to use absolute paths. In the future, we may develop a feature that supports absolute paths in a better way. The purpose of this PR is just to document the existing stop-gap measure so that others do not have to go on an odyssey to figure it out. :-)