Path aliases for imports in WebStorm
Solution 1
Yes, there is.
In fact, Webstorm can't automatically parse and apply Webpack config, but you can set up aliases the same way.
You just have to mark the parent folder of "utils" (in your example) as a resource root (right-click, mark directory as / resource root).
We just managed to do with the following structure :
/src
/A
/B
/C
We have A B and C folders declared as alias in Webpack. And in Webstorm we marked "src" as "Resource Root".
And now we can simply import :
import A/path/to/any/file.js
instead of
import ../../../../../A/path/to/any/file.js
while still having Webstorm correctly parsing and indexing all code, link to files, autocompleting and so on ...
Solution 2
I managed to set up aliases for WebStorm 2017.2 within webpack like this:
Solution 3
For the record: in PHPSTORM, working with laravel mix, I managed to solve this by creating a webpack.config.js file separately like:
const path = require('path')
const webpack = require('webpack')
module.exports = {
...
resolve: {
extensions: ['.js', '.json', '.vue'],
alias: {
'~': path.resolve(__dirname, './resources/assets/js')
}
},
...
}
And then importing it in the webpack.mix.js like:
const config = require('./webpack.config')
...
mix.webpackConfig(config)
Make sure the webpack configuration file is pointed correctly in the configuration of the PhpStorm in: Settings > Languages & Frameworks > Javascript > Webpack
Solution 4
You can define custom paths, so WebStorm/PhpStorm can understand your aliases. But make sure, they are identical with your aliases. Create file in your root directory and call it something like this: webStorm.config.js
(any js
file will be ok). Then configure your paths inside:
System.config({
"paths": {
"components/*": "./src/components/*",
"core/*": "./src/core/*",
...
}
});
WebStorm/PhpStorm will recognize System
as it's own module and will treat this file as configuration.
Solution 5
This is answered in a comment but to save people digging into comments and link only information, here it is:
As of WS2017.2 this will be done automatically. The information is here.
According to this, webstorm will automatically resolve aliases that are included within the webpack.config
in the root of the project. If you have a custom structure and your webpack.config
isn't in the root folder then go to Settings | Languages & Frameworks | JavaScript | Webpack
and set the option to the config you require.
Note: Most setups have a base
config which then call a dev
or prod
version. In order for this to work properly, you need to tell webstorm to use the dev one.

Bogdan D
Updated on January 07, 2022Comments
-
Bogdan D over 1 year
I use webpack path aliases for ES6 module loading.
E.g. If I define an alias for
utils
instead of something like
import Foo from "../../../utils/foo"
, I can do
import Foo from "utils/foo"
The problem is that once I start using aliases, WebStorm looses track of the import and I'm left with warnings and no auto-completion.
Is there a way to instruct WebStorm to use such aliases?
-
maddrag0n about 7 yearsthis will effectively break things when not directly building from Webstorm
-
Jalil about 7 yearsWhy is that ? If you keep the aliases in your Webpack configuration ... Actually we do build outside of Webstorm without any problem ...
-
Bogdan D about 7 yearsI haven't tested it yet, but the answer makes total sense. It was exactly what I was looking for when I asked the question. Thanks, @Jalil, I'm marking your answer "accepted"
-
Zation over 6 yearsI'ver tried in WebStorm 2016.2.3, but this solution doesn't work.
-
Hitmands over 6 yearsand what about imports in angular2
import { Component } from '@angular/core'
, that is resolved too. How can I implement theat
to make more clear that path is an alias? -
Jalil over 6 years@Zation I'm using Webstorm 2016.2.3 and it works pretty well. What problem do you have exactly ?
-
lima_fil over 6 yearsDon't forget to mark PARENT folder, not the starting folder of your path.
-
Jalil about 6 yearsYes. I've just bolded it.
-
anstarovoyt almost 6 yearsdeprecated answer. Starting since WS2017.2 webstorm automatically parses and applies Webpack config
-
thewoolleyman almost 6 yearsThis is the better answer. Relative paths are annoying, but the convenience of path aliases is not worth losing useful IDE functionality.
-
Jalil almost 6 yearsI added a deprecation notice.
-
Toosick almost 6 years@anstarovoyt I installed the Webstorm EAP 2017.2 and it still cannot figure out my aliases. Do I have to do something?
-
anstarovoyt almost 6 years@Toosick see this blogpost blog.jetbrains.com/webstorm/2017/06/…
-
rofrol almost 6 yearsWhat about multiple folders and multiple webpack configs? Doesn't work for me maybe because of this: "By default WebStorm will analyse the webpack configuration file in the root of the project, but you can select another file in Preferences | Languages & Frameworks | JavaScript | Webpack" blog.jetbrains.com/webstorm/2017/06/…
-
dericcain over 5 yearsThis seems to work unless a Webpack alias is used. I am using
@
as an alias to mysrc
folder, but even after pointing Webstorm to my config, it still would not resolve my imports correctly. However, once I marked thesrc
folder as Resource Root, it worked as expected. Would be nice if Webstorm could handle aliases, but no big deal. -
dericcain over 5 yearsAre you using a symbol for an alias like so:
'@': '../src')
. Also, are you using one file for your webpack config, or multiple? Wondering what is different. Thanks! -
webnoob over 5 yearsOne example of mine is
'@': path.resolve(__dirname, '../src/components')
. I use multiple files,webpack.base.conf.js
thendev
andprod
versions. They in turn call a config folder withindex.js
,dev.env.js
andprod.env.js
. I point my webstorm to look at thewebpack.dev.conf.js
file. -
dericcain over 5 yearsMy setup is very similar (vue-webpack-pwa) and pointing it to the
.dev.conf
did the trick. Pointing to the.base.conf
was not working. Good call! -
willredington315 over 4 yearsI think this may need an issue to be opened. I can confirm it works but oftentimes I'll open it and all of the webpack aliases are unresolved. I do an "Invalidate Caches and Restart" , and it works as advertised
-
Alexander about 4 yearsIt works. The file should include module.exports = {resolve: {alias: {'@utils': path.resolve(__dirname, '../utils/')}}}. Also I had to restart WebStorm to apply the changes.
-
Илья Зеленько about 4 yearsThanks! It helped me in PhpStorm 2019.1
-
Dave Johansen about 4 yearsHow do you get Webstorm to recognize
@someAlias
correctly? -
Moein Alizadeh almost 4 yearsWe should mark this answer to the best answer. Thanks.
-
TomTomSamSam almost 4 yearsthis one helped me
-
Alberto Perez almost 4 yearsThanks! Finally some solution for this annoying problem.
-
Daniel over 3 yearsWhere file you defined alias? In which file?
-
Kevin Bruccoleri over 3 yearsUsing the dev webpack config worked for me. Thanks!
-
joycollector about 3 yearsThis actually works! Really helped me with the project on Rollup+Svelte!
-
Mojtaba Hn almost 3 yearsthanks. it works. Q: Do you know any documentation for full options available for this file ?
-
Szczepan Hołyszewski over 2 yearsPlease consider the situation of users who don't actually use webpack in their project, and want to set up those two files SOLELY for the purpose of teaching PHPStorm how to resolve aliases. Those people won't know what should go in the "..." in order to make the webpack.mix.js file actually working for this purpose. Specifically, if I simply remove "...", the
mix
symbol is reported as unresolved by the IDE, which is a strong hint that it won't actually work. There are missing but critically important lines where the "mix" symbol is somehow required/imported. Please add those to your answer. -
Szczepan Hołyszewski over 2 yearsDon't rush to deprecate this answer. Accumulating experience shows that JetBrain's support for parsing Webpack config is fragile to the point of unusability. Old tricks must unfortunately still be used.
-
Matt over 2 yearsIs there a way to do this for a specific js file? So for example, I'd like to use "config" in place of "src/server/lib/config/index.js". I tried
"config": "./src/server/lib/config/index.js"
but that didn't work. -
Nhan DT about 2 yearsThis should be the best answer. Thank you very much.
-
Mac almost 2 yearsAwesome! Unfortunately, I also have to manually configure ESLint and Rollup with duplicates of the aliases. At least I have a clean environment, but I wish JetBrains would support Rollup natively like they do Webpack.
-
lyh543 over 1 year@Hitmands see stackoverflow.com/a/59369029/12208030
-
patricknelson about 1 yearThank you for posting this! This also works for the special
$lib/*
references in SvelteKit. A copy/paste answer for that can be found here: stackoverflow.com/questions/71552572/… -
D.R. 11 monthsLove your perfectionism. I'd do it identically (no joke).