Maiken Madsen
Maiken Madsen

Reputation: 651

'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?

Upvotes: 17

Views: 22794

Answers (7)

Jignesh Joisar
Jignesh Joisar

Reputation: 15145

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'

Upvotes: 1

sein_digital
sein_digital

Reputation: 19

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

Upvotes: 1

kubido
kubido

Reputation: 568

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'

Upvotes: 24

mrtns
mrtns

Reputation: 1

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

Upvotes: -2

duXing
duXing

Reputation: 1407

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

Upvotes: 3

Ian Callahan
Ian Callahan

Reputation: 41

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.

Upvotes: 4

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

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 

Upvotes: 19

Related Questions