17

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?

3
  • Use import VueRouter from "vue-router" and when defining use const router = new VueRouter({ your code}) Commented Jan 23, 2021 at 12:00
  • @YashMaheshwari this syntax is for vue 2 with vue router 3 Commented Jan 23, 2021 at 12:02
  • no worries, you could get that when you see that he's using vite which works only with vue 3 Commented Jan 23, 2021 at 12:05

7 Answers 7

24

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'
Sign up to request clarification or add additional context in comments.

3 Comments

This is the answer if you're referencing: router.vuejs.org/guide/#javascript
I had this issue in App.vue and this answer solve my issue.
This solves the issue but now it says VueRouter is not defined
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 

4 Comments

4.0.6 worked fine for me with vue@^3.0.5
NOTE: If you're running Vite you'll have to shut it down and restart it before this change takes effect. This did solve my problem.
@Roga do you mean quitting server and restarting again ?
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.

1 Comment

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!
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

Comments

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

Comments

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'

Comments

-2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.