I'm doing various projects in React.js with using awesome WebStorm by JetBrains.
Every time I type an existing component name WebStorm imports it automatically for me, which is awesome, but it is doing so by its relative path based on the file I'm doing the importation on.
Looks like so:
import vintageAxios from "../../../../services/api/index";
But I'm expecting it to be like:
import vintageAxios from "services/api/index";
I already setup my .env.local
file with the NODE_PATH=src/
variable, I've used create-react-app to setup the project.
EDIT: I'm not having problems with the importations. What I want to know is if there is a way to configure WebStorm so it automatically does the importations with absolute instead of relative paths.
With auto-import tooltip If this checkbox is selected, WebStorm shows a tooltip with an import suggestion when you position the caret at an unresolved symbol that can be imported. When the checkbox is cleared, you can invoke import generation only be pressing Alt+Enter .
If we want to go a step further and sort the imports, we can go to Preferences / Settings | Editor | Code Style | JavaScript or TypeScript and select Sort imports by modules on the Imports tab.
The option “Do not import exactly from” allows you to tell the IDE that you don't want any imports that are from the specific path (e.g. @material-ui/core ) or path pattern (e.g. @material-ui/core/** ). The IDE will then use an alternative path if there is one.
In the Settings/Preferences dialog ( Ctrl+Alt+S ), click Editor | General | Auto Import. Select the Add unambiguous imports on the fly checkbox, and apply the changes.
Found it (WebStorm 2017.2), after marking your source directory (ex. /src) as the source root (right click it to find that), you can then check this box:
For anyone using Typescript. Check the option Use paths relative to tsconfig.json
in Editor
> Code Style
> Typescript
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With