16

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Maiken Madsen
  • 611
  • 1
  • 15
  • 29

7 Answers7

23

if you check inside the file '/node_modules/.vite/vue-router/dist/vue-router.esm.js?v=4830dca4'

there is no export default syntax. only named export {}

so instead import default, use named import.

// don't
import VueRouter from 'vue-router'

// do
import { createRouter, createWebHashHistory } from 'vue-router'
kubido
  • 546
  • 6
  • 11
19

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 
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164
4

For what it's worth I ran into this same issue while using vue-router@4. I was using yarn instead of npm. Switching to npm fixed the issue. I'm not sure if the problem was yarn or if a fresh install was all that was needed.

  • Me too. I have `vue-router@4` and doing it with `yarn`. From your comment, I tried just deleting `node_modules` and reinstalling with `yarn` again and worked! – juanesarango Apr 09 '21 at 17:32
3

if you update vue-router from v3.x to v4.x under your vite project,plz clear the deps cache

vite --force

offical docs: https://vitejs.dev/guide/dep-pre-bundling.html#caching

duXing
  • 1,377
  • 1
  • 10
  • 24
1

There is an easy solution if exported module actually exports only methods, but you still want to import all of them as an object.

// if this does not work
import VueRouter from 'vue-router'

// try this
import * as VueRouter from 'vue-router';

This usually happens when you import compiled JS into TS. Same solution works with underscore or lodash

sudo-sein
  • 11
  • 1
1

Don't import like this in vue3 it will not work

import VueRouter from 'vue-router';

Import Like this it will work

import * as VueRouter from 'vue-router';

or Import Specific Then do like this

import { createRouter, createWebHashHistory } from 'vue-router'
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
-1

Try to close your app than re open again, I fix this issue

mrtns
  • 1