Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React with JetBrains WebStorm auto import with absolute path instead of relative

Tags:

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.

like image 590
Cristian Rojas Avatar asked Sep 29 '17 12:09

Cristian Rojas


People also ask

How do you auto import on WebStorm?

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 .

How do I sort imports in WebStorm?

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.

Do not import exactly from?

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.

How do I enable auto import dependencies in Intellij?

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.


2 Answers

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:

enter image description here

like image 195
ABCD.ca Avatar answered Sep 20 '22 15:09

ABCD.ca


For anyone using Typescript. Check the option Use paths relative to tsconfig.json in Editor > Code Style > Typescript

like image 26
NearHuscarl Avatar answered Sep 19 '22 15:09

NearHuscarl