At vite vue runs the error – Why the error runs – Uncaught SyntaxError: The requested module ‘/src/router/index.js’ does not provide an export named ‘default’ at main.js vue ? What is wrong ?
// main.js
import { createApp } from 'vue'
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap/dist/js/bootstrap.js'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
app.mount('#app')
// router index.js
import VueRouter from 'vue-router';
import Home from '../views/Home.vue';
import Shops from '../views/Shops.vue';
const router = VueRouter.createRouter({
history: VueRouter.createWebHashHistory(),
routes: [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/shops',
name: 'Shops',
component: Shops,
},
],
});
export default router;
2
Answers
Can you please share
/src/router/index.js
code? What I can see in the code you have shared via stackblitz is index.js inside router folder is empty.Please add an export inside router/index.js, for example refer below
These are all types of export default you can use..
Please refer MDN Docs to know more about how to use export
https://developer.mozilla.org/en-US/docs/web/javascript/reference/statements/export
Edit Solution :
import * as VueRouter from 'vue-router';
Update this in router/index.js and it should work. This error occurs because vue-router has a named export instead of a default export
you must declare the router in the following manner and export from the router file (src/router/index.js).
as you see the last line is exporting the defined router file to the the component that you want to import and use it as a router.