You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
alias '@' to path.resolve(__dirname, './src') is not working #279
I think we probably will eventually provide an easier way to alias src directories. If your resolver is not working, it's better to provide a full reproduction instead of a single config file.
Reacted by Asko Kauppi, Max Lee, Michael, 戈志刚, Mario34, Ian Dunn, René Sinnbeck, Marios Antonoudiou, Michael Tran, Natalie Perret and 6 more
Reacted by Max Lee, Michael, simultsop, Natalie Perret, lm-lambda, Evgeny Labuzov, lgs821, ahyagoub40 and Samuel Denis-D'Ortun
@yyx990803
that should work, but I personally don't like to do that, because a path starts with / make me thought it's a file under the / folder of my file system.
And could you review my code of the resolver, am I doing something wrong or it's a bug of vite?
Reacted by phanmn, Ahmed Elrefaey, liulun, monkeytao, Faizal Andyka, spking11, Brendon Matos, Douglas Wille, Andrius K, richard1015 and 20 more
Reacted by Ahmed Elrefaey, Christopher Caldwell, KishiTheMechanic, Brendon Matos, Douglas Wille, Ian Dunn, Michael Tran, Paulo Cuambe, Gabriele Tomberli, Evgeny Labuzov and 1 more
Reviewing my code I've figured out that my scenario is a bit different... I mean:
I've tested that using the alias works like a charm when index.html is on the root of the project, but does not work when moving index.html into src (using vite serve src)
Also noticed that when moving the index.html into src folder, I can import files from root (p.e. import App from '/App') which is great. The bad point is that IntelliSense is not useful...
This issue has been locked since it has been closed for more than 14 days.
If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.
Activity
remyz17 commentedon May 27, 2020
Vite alias don't work like webpack does.
You need to use resolvers to do what you want to do.
hronro commentedon May 27, 2020
@remyz17
I tried this code, but it doesn't work either:
remyz17 commentedon May 27, 2020
I guess you need to implement this 2 functions.
requestToFile and fileToRequest
Take a look at this code
https://github.com/vuejs/vitepress/blob/master/src/node/resolver.ts#L19
remyz17 commentedon May 27, 2020
i tried to do it and it's not fully working.
For exemple it don't work inside vue template:
<img src="/@/..." />I noticed that we can not use @/ because it’s recognised as a node module so I used /@/
hronro commentedon May 27, 2020
@remyz17
This not works for me. In my case, the function
fileToRequestis not even be called, and I'm still figuring out why.remyz17 commentedon May 27, 2020
Send your vite config so i can take a look
hronro commentedon May 27, 2020
@remyz17
OK, here is my config:
yyx990803 commentedon May 27, 2020
I think we probably will eventually provide an easier way to alias src directories. If your resolver is not working, it's better to provide a full reproduction instead of a single config file.
hronro commentedon May 28, 2020
@yyx990803
https://codesandbox.io/s/vite-src-alias-demo-60931
yyx990803 commentedon May 28, 2020
btw you don't need an alias like that... you can just do
/main.jsbecause that's the server root.hronro commentedon May 28, 2020
@yyx990803
that should work, but I personally don't like to do that, because a path starts with
/make me thought it's a file under the/folder of my file system.And could you review my code of the resolver, am I doing something wrong or it's a bug of vite?
yyx990803 commentedon May 28, 2020
It's a bug. It's fixed in 801951e but it also adds an easier way to alias a directory so you can just do
hronro commentedon May 29, 2020
@yyx990803
Still doesn't work for after I upgrade vite to v0.18.0.
https://codesandbox.io/s/vite-src-alias-demo2-lzpm0
22 remaining items
csulit commentedon Apr 15, 2021
TS is still flags the import. I don't know if this is the right way to do it, I already solve the issue by declaring this to tsconfig.json
nzhtrtlc commentedon Apr 15, 2021
Can't we just define a resolver that resolves src folder so we don't have to use aliases ?
import Button from components/Button;MyViteProject > src > components > Button
how can resolve src folder in vite.config.js ?
csulit commentedon Apr 15, 2021
I solve it by declaring this to tsconfig.json
The error is gone and did not break any component in build.
slumtrimpet commentedon Apr 20, 2021
For what it's worth, adding the following to tsconfig.json makes typescript happy:
BUT you still need to also add corresponding:
to vite.config.js to make vite happy. Maybe that's obvious to most, but it wasn't to me. ;-)
jgermade commentedon Apr 24, 2021
Still no working for me with
reactRefreshwhennpm run devIt works when
npm run buildand in any case when using a --template vue (@vitejs/plugin-vue)This is my vite.config.js:
csulit commentedon Apr 24, 2021
Mine is working fine with this config.
jgermade commentedon Apr 26, 2021
Thanks @csulit for your reply!
Reviewing my code I've figured out that my scenario is a bit different... I mean:
I've tested that using the alias works like a charm when index.html is on the root of the project, but does not work when moving index.html into src (using
vite serve src)Also noticed that when moving the index.html into
srcfolder, I can import files from root (p.e.import App from '/App') which is great. The bad point is that IntelliSense is not useful...Using this jsconfig.json:
{ "compilerOptions": { "baseUrl": "./", "paths": { "@/*": ["src/*"] } } }Gets folloging sugestions:
But when using root slash:
Any ideas how to use this alias moving index.html into src folder?
vite.config.jsSarcevicAntonio commentedon May 2, 2021
This worked for me! Using vite with Svelte and Typescript and this setup works perfectly with VSCode importing my files automatically.
Thanks a bunch slumtrimpet
ddzy commentedon May 11, 2021
Working for me
marialovesbeans commentedon May 17, 2021
Is there a way of specifying
srcas root import path? ex: increate-react-appyou can set a baseUrl, so instead of writingYou can write:
This looks much cleaner. How to achieve the same in Vite?
marxangels commentedon May 19, 2021
idea.config.js
npearson72 commentedon Jun 11, 2021
The following is working for me:
zhuchangfeng commentedon Jun 22, 2021
vite 1 verson
alias: { '@pages': path.resolve(__dirname, './src/pages') }vite 2 verson
resolve:{ alias: [ { find: '@', replacement: resolve('src') , },] },crucial step
yarn add @vitejs/plugin-vueyarn upgrade vite@2.3.8vite.config.js
import vue from '@vitejs/plugin-vue'import { defineConfig } from 'vite'export default defineConfig({resolve:{alias: [ {find: '@',replacement: resolve('src') ,}]}})github-actions commentedon Jul 14, 2021
This issue has been locked since it has been closed for more than 14 days.
If you have found a concrete bug or regression related to it, please open a new bug report with a reproduction against the latest Vite version. If you have any other comments you should join the chat at Vite Land or create a new discussion.