Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') #496

Closed
charismafight opened this issue Aug 30, 2024 · 10 comments

Comments

@charismafight
Copy link

Hi there,
I created a new vue project by 'vue create' to test this package, followed the installation and migrating part, I've got 'Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')' error from router.ts. I found out that routes imported from 'vue-router/auto-routes' is undefined. And I have also created another project using vite, and it worked correctly. The only difference is the vite.config.ts and vue.config.js. Anybody can help me solving this problem? Thanks.

vue.config.js

module.exports = {
  configureWebpack: {
    resolve: { extensions: [".ts", ".tsx", ".js", ".json"] },
    plugins: [
      require('unplugin-vue-router/webpack')({
        /* options */
      }),
    ],
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          loader: 'ts-loader',
          exclude: /node_modules/,
          options: {
            appendTsSuffixTo: [/\.vue$/],
          }
        }
      ]
    }
  },
}

tsconfig.json

{
  "include": [
    // other files...
    "./typed-router.d.ts"
  ],
  "compilerOptions": {
    "moduleResolution": "Bundler",
    "target": "es2016",
    "module": "commonjs", /* Specify what module code is generated. */
    "types": [
      "unplugin-vue-router/client"
    ],
    "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
    "strict": true, /* Enable all strict type-checking options. */
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}

typed-router.d.ts

/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-router. ‼️ DO NOT MODIFY THIS FILE ‼️
// It's recommended to commit this file.
// Make sure to add this file to your tsconfig.json file as an "includes" or "files" entry.

declare module 'vue-router/auto-routes' {
  import type {
    RouteRecordInfo,
    ParamValue,
    ParamValueOneOrMore,
    ParamValueZeroOrMore,
    ParamValueZeroOrOne,
  } from 'vue-router'

  /**
   * Route name map generated by unplugin-vue-router
   */
  export interface RouteNamedMap {
    '/': RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>>,
  }
}

tsconfig.json

{
  "include": [
    // other files...
    "./typed-router.d.ts"
  ],
  "compilerOptions": {
    "moduleResolution": "Bundler",
    "target": "es2016",
    "module": "commonjs", /* Specify what module code is generated. */
    "types": [
      "unplugin-vue-router/client"
    ],
    "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
    "strict": true, /* Enable all strict type-checking options. */
    "skipLibCheck": true /* Skip type checking all .d.ts files. */
  }
}

router.ts

import { createRouter, createWebHistory } from 'vue-router'
import { routes } from 'vue-router/auto-routes'

export const router = createRouter({
    history: createWebHistory(),
    routes,
});

main.js

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

createApp(App)
    .use(router)
    .mount('#app')
@posva
Copy link
Owner

posva commented Aug 30, 2024

There are probably some updates needed for webpack. See #488 .
For the moment, use Vite. It's the recommended bundler for new projects too.

@posva posva closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2024
@charismafight
Copy link
Author

npm list webpack showed '[email protected]', I reinstalled it and still not worked, any further suggestion?
In fact, I want to migrate an exsiting project and keep the deployment config, so I create a new project with webpack just for test.

@zhangmo8
Copy link

zhangmo8 commented Sep 1, 2024

npm list webpack showed '[email protected]', I reinstalled it and still not worked, any further suggestion?

In fact, I want to migrate an exsiting project and keep the deployment config, so I create a new project with webpack just for test.

Is it convenient to post a whole smallest project to help me understand and solve the error report?

@charismafight
Copy link
Author

npm list webpack showed '[email protected]', I reinstalled it and still not worked, any further suggestion?
In fact, I want to migrate an exsiting project and keep the deployment config, so I create a new project with webpack just for test.

Is it convenient to post a whole smallest project to help me understand and solve the error report?

@zhangmo8 Sure, thanks a lot.
vuerouter.zip

@zhangmo8
Copy link

zhangmo8 commented Sep 4, 2024

npm list webpack showed '[email protected]', I reinstalled it and still not worked, any further suggestion?
In fact, I want to migrate an exsiting project and keep the deployment config, so I create a new project with webpack just for test.

Is it convenient to post a whole smallest project to help me understand and solve the error report?

@zhangmo8 Sure, thanks a lot. vuerouter.zip

It's working normally. Make sure eslint works when you give us the minimum demo @charismafight
vuerouter66.zip

@charismafight
Copy link
Author

It's working normally.

That's weird, I've tried your version and it's still not worked. ( I just use npm intall and npm run serve)
image

image

Make sure eslint works when you give us the minimum demo @charismafight

Okay, I will note that.

@zhangmo8
Copy link

zhangmo8 commented Sep 4, 2024

image What's your Node version and npm version @charismafight

@charismafight
Copy link
Author

@zhangmo8
node v20.13.1
npm 10.8.2

@zhangmo8
Copy link

zhangmo8 commented Sep 5, 2024

It's strange that I can run normally in my local.

@zam157
Copy link

zam157 commented Sep 6, 2024

Are you on Windows? I have the same problem using rspack on Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants