vite: alias '@' to path.resolve(__dirname, './src') is not working
Describe the bug
Alias { '@': path.resolve(__dirname, './src') } is not working.
Reproduction
project structure:
vite.config.js
src
├── app.js
├── index.js
vite.config.js:
const path = require('path');
module.exports = {
alias: {
'@': path.resolve(__dirname, './src'),
}
};
src/index.js
import { Foo } from '@/app';
Got error in vite:
[vite] Failed to resolve module import "@/app"
System Info
- required
viteversion: 0.17.0 - required Operating System: Linux
- required Node version: 14.3.0
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 49 (12 by maintainers)
I think you are just missing the
const path = require('path');part. Make sure to also add the leading “/” when importing components in the Vue script section, i.e.:import Container from "/@/views/Container.vue";Edit: I also used
path.resolve(__dirname, 'src')instead ofpath.resolve(__dirname, './src')but that should not matter.Edit2: You could also use the find and replace option to alias the
@, i.e.:then again you don’t need the leading slash when importing.
Edit 3: Since vite 2.0.1 introduces the
resolvesyntax, use: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
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.
You have to import as
/@/xxx, it cannot start with@because that’s considered a package.Note Vite is not a bundler - it’s a dev server. Any request not starting with
/or.is considered a module dependency request. All other requests must be valid HTTP requests (start with/or.).This also means you can NOT directly import from fs paths like in webpack:
This doesn’t make sense in Vite. So anything starting with
/is a request from therootwhere the server is serving.Having trouble with aliases here after upgrading to
2.0.1.vite.config.tsAnd then it errors in places such as:
import Nav from '@/components/Nav.vue',component: () => import('@/views/About.vue')The error message says
the plugin "vite:dep-scan" didn't set a resolve directory, but I can’t find anything related tovite:dep-scananywhere on the internet… 🤔I’ve tried the various
@rollup/plugin-aliasconfig options syntax from (here) as well as with and without thedefineConfig()wrapper - all no luck and generating the same error message.Heads up I was porting an app and
/@/xxxaliasing working for all code but the assets did not work.Working for me
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. 😉
@ndom91 Does it work when you delete the
/fromfind: '@/'?does not work
The Asset Handling section of the vite documentation is probably what you’re looking for. Your logic is flawed (I think) since vite doesn’t get the chance to transform
/@/assets/logoSmall.pngor/@/assets/logo.png. I haven’t used vite in a few months but I think something like this might work:This solution assumes that you have the alias correctly configured. Make sure to read through Alias Behavior Change.
source config
With the
resolve.aliasconfig in 2.0.1, you can use@/directy instead of/@/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:
Gets folloging sugestions:
But when using root slash:
Any ideas how to use this alias moving index.html into src folder?
vite.config.jsI solve it by declaring this to tsconfig.json
The error is gone and did not break any component in build.
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
It makes sense to post a comment with a clear solution for others to find, that is not a problem 👍🏼
But asking questions against a closed bug report is not helpful because the context refers to an older version of Vite, and it doesn’t allow proper tracking of concrete bugs in newer versions. Issues are not intended to be used for discussions, we have better platforms for that with GitHub Discussion and Vite Land. If a good solution to something that is not clear in this issue is identified there, it is a good idea to link it from here for later reference.
Thanks @jsmith,
Your solution worked well and I learnt something new.
Thanks