Encountering the trouble of "Uncaught TypeError: Object(...) is not a function" when working with Vue 3 and the Vue router

Started a new Vue project using the CLI by running:

vue create my-project

Decided to incorporate two pages, so I installed the latest version of vue-router (currently v3.4.8) and followed a helpful Vue Mastery tutorial on routing.

This is how my router.js file is structured:

import { createWebHistory, createRouter } from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    { path: '/', name: 'Home', component: Home },
    { path: '/about', name: 'About', component: About },
  ]
})

export default router

In addition, this is what my main.js file looks like:

import { createApp } from 'vue'
import router from './router'

createApp({
  template: `
  <div>
    <router-link to='/'>Home</router-link>
    <router-link to='/create'>Create</router-link>
  </div>
  `
})
.use(router)
.mount('#app')

The Home and About components are quite basic in structure, as seen below:

<template>
  <div>TODO: Home</div>
</template>

<script>
  export default {
    name: 'Home'
  }
</script>

Unfortunately, an error has surfaced with the message:

Uncaught TypeError: Object(...) is not a function

at eval (router.js?41cb:5)

This issue specifically arises within the createRouter function call.

Could there be an error in my implementation?

Edit: after feedback from Boussadjra Brahim, I realized that originally createWebHistory was not being called as a function. I have since updated the code accordingly.

Coincidentally, once rectified, the error no longer occurs during its invocation.

Answer №1

The problem arises when Vue router 3 is installed with Vue 3, so the solution is to remove the current version:

npm uninstall vue-router --save

Then, install the new version by running:

npm i vue-router@next --save

Check out this example

Answer №2

My situation was a bit different as I encountered the same error. I originally had Vue 2.6 and Vue Router 4 installed, but I had to downgrade Vue Router to version 3.5 in order to resolve the issue.

Answer №3

If you're working with vue3, make sure to add the @4 package by running this command:

npm install vue-router@4

If you encounter any issues, be sure to check out this migration guide: Transitioning from Vue 2

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance the appearance of the initial division in the Bootstrap slider by incorporating an additional class using PHP with ACF (Advanced Custom Fields

I am attempting to create a bootstrap slider using an ACF repeater field with 3 slider images. Below is the code I am currently using: <?php if( have_rows('text_block_repeater') ): ?> <div id="carouselExampleControls" class= ...

How to display an image in a pop-up with a title

I am currently using the Bootstrap framework and I am trying to create a popup image with a title, but it only shows the image. I am not sure how to add code in JavaScript to display the title as well. Can someone please assist me? Here is my code: Javas ...

Issues with incorrect source path in Typescript, Gulp, and Sourcemaps configuration

In my nodejs app, the folder structure is as follows: project |-- src/ | |-- controllers/ | | |`-- authorize-controller.ts | |`-- index.ts |--dist/ | |--controllers/ | | |`-- authorize-controller.js | | |`-- authorize-controller.js.map | ...

Nuxt: How to access i18n.localePath in middleware functions

I have created a customized middleware in Nuxt as follows: export default function({ store, redirect, app }){ if (!store.state.isAuth) { return redirect(app.i18n.localePath('/auth')) } } When a user is not authenticated, they are ...

The concealed [hidden] attribute in Angular2 triggers the display of the element after a brief delay

I am currently utilizing the [hidden] attribute within my application to conceal certain elements based on a specific condition. The situation is such that I have two divs - one for displaying results and another for showing the text "No results". Both t ...

I've encountered some issues with importing pagination from modules after installing SwiperJs

Having some issues with importing pagination from modules in SwiperJs for my nextjs project. The error message "Module not found: Package path ./modules is not exported from package" keeps popping up. I have tried updating the module to the latest version ...

Solution for problem with IE scrollWidth

Encountered a peculiar bug in IE where the scrollWidth is consistently off by 1px compared to the offsetWidth. This anomaly appears to be triggered by the length of text/characters within the element, specifically when overflow is set to something other th ...

Tips for creating a new tab or window for a text document

Below code demonstrates how to open a new tab and display an image with a .jpg extension. Similarly, I want to be able to open a text document (converted from base64 string) in a new tab if the extension is .txt. success: function(data) { var ...

Creating a custom date selection component in Angular 2 RC1

Can anyone recommend a datepicker that is compatible with Angular 2 RC1? I noticed that ng2-datepicker seems to be using angular2 RC1, but when trying to install it, it's asking for Angular 2 Beta. Would appreciate any assistance. Thank you in advan ...

Updating global variable in JavaScript at inappropriate times

Within this code: window.g_b_editEnable = false; window.g_a_PreEditData = 'hello'; function EditRow(EditButton){ if(!window.g_b_editEnable){ window.g_b_editEnable = true; var a_i_Pos = a_o_table.fnGetPo ...

Discover the step-by-step guide to integrating pagination API in Next JS using getServerSideProps

I need to update a parameter that currently reads page=1, it should instead be set as page=${setPage} Then, when the button is pressed, it triggers a change in the table data. This is how my code looks in [table].js: How can I achieve this? Should I use ...

Looking for an API to retrieve random quotes and images from a website?

Greetings, my name is Antika. I recently embarked on a coding journey and have been focusing on learning HTML/CSS along with the basics of JS. Level of Expertise: Beginner During my exploration, I stumbled upon this intriguing website - . It stands out a ...

Pass the outcome back to the calling function

I've been struggling with this issue for a while. I am working with ion-autocomplete and trying to retrieve data using a factory. The Factory I'm using is as follows: myApp.factory('items', function($http){ return { list: fun ...

Tips for positioning a picklist field dropdown on top of a lightning card in Lightning Web Components

Attempting to resolve an issue with CSS, I have tried adding the following code: .table-responsive, .dataTables_scrollBody { overflow: visible !important; } However, the solution did not work as expected. Interestingly, when applying a dropdown pickli ...

Setting a timeout from the frontend in the AWS apigClient can be accomplished by adjusting the

I am currently integrating the Amazon API Client Gateway into my project and I have successfully set up all the necessary requests and responses. Now, I am trying to implement a timeout feature by adding the following code snippet: apigClient.me ...

Achieving sequential actions in Javascript without the need for state updates

One aspect of jQuery that I find impressive is its ability to chain methods like .animate() and .css(). This is made possible by utilizing the special variable "this" in the backend code. I am interested in implementing a similar method chaining mechanism ...

What is the best way to notify the price range slider value using jQuery?

I am trying to implement a price range slider on my website. However, when I slide the range slider, instead of getting the value in the alert box, it shows [objectobject]. I am not sure why this is happening. Can someone help me figure out how to display ...

Issue with populating JsonRest store in Dojo Enhanced Datagrid

My data grid is not loading the information from the store The response I get from my REST call looks like this: {"onlineUsers":[],"offlineUsers":["123.456.7.890:8080"]} This is what my code looks like: require([ "dojo/store/JsonRest", "dojo/sto ...

Making a column in a Vue data grid return as a clickable button

My goal is to utilize vue.js grid to display multiple columns with calculated text values, along with a clickable column at the end that triggers a dynamic action based on a parameter (such as calling an API in Laravel). However, when I include the last c ...

Dealing with errors while using the axios method in Vue.js

When receiving a response like this- data: Array(0) length: 0 What is the best way to handle this error and display a message indicating that no matching data was found in vuejs? ...