Search code examples
vue.jsvue-routervuejs3vue-router4vite

'does not provide an export named 'createRouter'' vue 3, vite and vue-router


I just started using vite with vue.

When I'm trying to use vue-router I get the error:

SyntaxError: The requested module '/node_modules/.vite/vue-router/dist/vue-router.esm.js?v=4830dca4' does not provide an export named 'createRouter

My router/index.js looks like this:

import {
 createWebHistory,
 createRouter
} from "vue-router";

import Services from "../views/Services.vue";
import Customers from "../views/Customers.vue";

const history = createWebHistory();
const routes = [
  {
    path: "/",
    component: Services
  },
  {
    path: "/customers",
    component: Customers
  },
];
const router = createRouter({
  history,
  routes
});
export default router;

My main.js looks like this:

import { createApp } from 'vue'
import App from './App.vue'
import './index.css'
import router from './router'

createApp(App).use(router).mount('#app')

My package.json looks like this:

{
 "name": "frontend",
 "version": "0.0.0",
 "scripts": {
 "dev": "vite",
 "build": "vite build"
},
 "dependencies": {
 "vue": "^3.0.5",
 "vue-router": "^3.4.9"
},
 "devDependencies": {
 "@vitejs/plugin-vue": "^1.0.4",
 "@vue/compiler-sfc": "^3.0.5",
 "autoprefixer": "^10.2.3",
 "postcss": "^8.2.4",
 "tailwindcss": "^2.0.2",
 "vite": "^2.0.0-beta.12"
 }
}

Anyone knows how to export the route?


Solution

  • You should uninstall the current vue-router module and reinstall the latest (version 4) one which is compatible with Vue 3 by running :

    npm uninstall vue-router
    

    then

    npm install vue-router@next -S