10

I created a vue 3 project with vite and I wanted to add vue-router to the project, so from the terminal I wrote vue add router but after downloading everything I get the following error:

Error: Cannot find module '@vue/cli-service/generator/template/src/App.vue' from '/home/frostri/projects/onedrive_local/client/node_modules/@vue/cli-plugin-router/generator/template/src'
    at Function.resolveSync [as sync] (/usr/lib/node_modules/@vue/cli/node_modules/resolve/lib/sync.js:102:15)
    at renderFile (/usr/lib/node_modules/@vue/cli/lib/GeneratorAPI.js:515:17)
    at /usr/lib/node_modules/@vue/cli/lib/GeneratorAPI.js:300:27
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Generator.resolveFiles (/usr/lib/node_modules/@vue/cli/lib/Generator.js:268:7)
    at async Generator.generate (/usr/lib/node_modules/@vue/cli/lib/Generator.js:175:5)
    at async runGenerator (/usr/lib/node_modules/@vue/cli/lib/invoke.js:111:3)
    at async invoke (/usr/lib/node_modules/@vue/cli/lib/invoke.js:92:3)

Is there anything I can do to fix it?

3 Answers 3

20

The vue command is from Vue CLI. Vue CLI commands (i.e., vue add router) are not intended for Vite projects despite the similarities in project structure. There currently isn't an official Vite CLI command to automatically augment your project files to setup vue-router, so you'll have to do it manually:

  1. Run this command from the project root to install vue-router (version 4x for Vue 3):

    npm i -S vue-router@4
    # or:
    yarn add vue-router@4
    
  2. Create src/router.js with the following contents:

    import { createRouter, createWebHistory } from 'vue-router'
    import HelloWorld from './components/HelloWorld.vue'
    
    export default createRouter({
      history: createWebHistory(),
      routes: [
        {
          path: '/',
          component: HelloWorld,
        }
      ]
    })
    
  3. Edit src/main.js to install the router instance:

    import { createApp } from 'vue'
    import App from './App.vue'
    import './index.css'
    import router from './router' 👈
    
    createApp(App)
      .use(router) 👈
      .mount('#app')
    
  4. Edit src/App.vue to contain:

    <template>
      <router-view />
    </template>
    
Sign up to request clarification or add additional context in comments.

1 Comment

This saved me a lot of time. Thank you so much, this should be in their official docs!
18

I don't know if this helps, but at least works for me.

first I installed @vue/cli-service

npm install --save-dev @vue/cli-service

and then Vue Router.

vue add router

Let me know if this works for you! Have a nice day!

1 Comment

This worked for me! I had installed vue cli globally: npm i -g @vue/cli, but not in the project I think. (Also I was trying to add typescript, not router, but I ended up here)
2

I have followed the above steps and it worked for me

sudo npm install --save-dev @vue/cli-service

npm fix audit

then add router using the command

vue add router

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.