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

Closed
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+28 −0
Diff settings

Always

Just for now

@@ -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:

This comment has been minimized.

@gaearon

gaearon Sep 21, 2016

Member

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.

@gaearon

gaearon Sep 21, 2016

Member

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:

This comment has been minimized.

@gaearon

gaearon Sep 21, 2016

Member

Let’s put NODE_PATH into backticks so that it’s highlighted.

@gaearon

gaearon Sep 21, 2016

Member

Let’s put NODE_PATH into backticks so that it’s highlighted.

This comment has been minimized.

@gaearon

gaearon Sep 21, 2016

Member

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.”

@gaearon

gaearon Sep 21, 2016

Member

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.”

```js
import Banana from 'fruits/Banana'; // fruits is a subdirectory of src
```
If you use Bash on OS X or Linux:
```js
NODE_PATH=./src npm start

This comment has been minimized.

@gaearon

gaearon Sep 21, 2016

Member

Maybe let’s recommend doing this inside package.json scripts instead?

@gaearon

gaearon Sep 21, 2016

Member

Maybe let’s recommend doing this inside package.json scripts instead?

This comment has been minimized.

@gaearon

gaearon Sep 21, 2016

Member

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.

@gaearon

gaearon Sep 21, 2016

Member

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.

NODE_PATH=./src npm run build
NODE_PATH=./src npm test
```
If you use Cmd on Windows:
```js
NODE_PATH=./src&&npm start
NODE_PATH=./src&&npm run build
NODE_PATH=./src&&npm test
```
Note that lack of whitespace on Windows is intentional.
### `DangerButton.js`
ProTip! Use n and p to navigate between commits in a pull request.