question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Offer a way to import files relative to `src/`

See original GitHub issue

Not really sure about this, but I find it quite annoying to have imports that look like:

import mock from "../../../testUtils/myMock"

Relative paths are good for resources that will always live next to the calling file but when referring to shared helpers/modules I find them quite counter-productive.

It would be possible to configure webpack so we could import files from src/ just like import 'testUtils/myMock or maybe src/testUtils/myMock by adding src to the resolve root.

Would it make sense ? Maybe relative paths are not that bad and I’m missing something ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

17reactions
gaearoncommented, Sep 5, 2016
  1. Put NODE_PATH=./src before every script command in package.json
  2. Now you can write from 'entities/posts' assuming entities exists inside src.
13reactions
bikesheddercommented, Jun 21, 2018

I quite like the idea how Vue solved it in vue-cli: They configured webpack to replace @/ by the source directory using resolve.alias. I quite like the idea and typing import "@/base/components/FooBar" doesn’t look too bad. @ on its own should never be a valid scope and should be pretty future proof.

How about adopting that idea to create-react-app as well?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ask Question - Stack Overflow
You should be able to import anything inside src without relative paths. Note I would not recommend calling your folder src/redux because ...
Read more >
Absolute imports with Create React App | by David Gilbertson
I think it's reasonable to say that sibling files should be imported with a relative path, but not anything where you need to...
Read more >
Why and How to Use Absolute Imports in React
Using absolute imports to better organize your React project is a great way. Relative imports are hard to follow and break during ...
Read more >
Use Absolute Paths with React - Better Programming
By default, relative paths are the supported way of importing modules ... For instance, all modules that live inside src/components/ can now ...
Read more >
Auto import | PyCharm Documentation - JetBrains
In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. · In the Python section, configure automatic imports:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found