Cannot start nuxt: 无法读取未定义的属性 (读取’sitemap’)

huangapple go评论108阅读模式
英文:

Cannot start nuxt: Cannot read properties of undefined (reading 'sitemap')

问题

在你的项目中,安装了 @nuxtjs/sitemap 插件后,运行时遇到了错误。可能是由于版本不匹配引起的。请确保你的依赖版本与插件的要求相符。

英文:

I'm working on my first nuxt js project and I've a small problem. I installed a sitemap in my project using "npm install @nuxtjs/sitemap" and I've an error that I don't know how to fix.

package.json

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. {
  2. &quot;name&quot;: &quot;nuxt-app&quot;,
  3. &quot;private&quot;: true,
  4. &quot;scripts&quot;: {
  5. &quot;build&quot;: &quot;nuxt build&quot;,
  6. &quot;dev&quot;: &quot;nuxt dev&quot;,
  7. &quot;generate&quot;: &quot;nuxt generate&quot;,
  8. &quot;preview&quot;: &quot;nuxt preview&quot;,
  9. &quot;postinstall&quot;: &quot;nuxt prepare&quot;
  10. },
  11. &quot;devDependencies&quot;: {
  12. &quot;nuxt&quot;: &quot;^3.3.3&quot;,
  13. &quot;nuxt-simple-sitemap&quot;: &quot;^2.4.8&quot;,
  14. &quot;sitemap&quot;: &quot;^7.1.1&quot;
  15. },
  16. &quot;dependencies&quot;: {
  17. &quot;@nuxtjs/bootstrap-vue&quot;: &quot;^2.0.4&quot;,
  18. &quot;@nuxtjs/sitemap&quot;: &quot;^2.4.0&quot;,
  19. &quot;@nuxtjs/tailwindcss&quot;: &quot;^6.1.3&quot;,
  20. &quot;bootstrap-vue&quot;: &quot;^2.23.1&quot;
  21. }
  22. }

<!-- end snippet -->

nuxt.config.ts

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // https://nuxt.com/docs/api/configuration/nuxt-config
  2. export default defineNuxtConfig({
  3. modules: [
  4. &#39;@nuxtjs/tailwindcss&#39;,
  5. &#39;@nuxtjs/sitemap&#39;,
  6. ],
  7. })

<!-- end snippet -->

This is the error I've when I try to run the project
Cannot start nuxt: 无法读取未定义的属性 (读取’sitemap’)

Thanks for your help !

答案1

得分: 1

@nuxtjs/sitemap 现在不再与 Nuxt3 兼容。

对于简单的静态网站

您可以使用 Nuxt3 的新 simple-sitemap 模块快速生成您的站点地图。您可以按照它们的指示在此处进行操作

如果您有动态路由(如 /blog/[slug].vue)

您可以仅使用 sitemapJS 插件生成您的 sitemap.xml。

  1. npm install --save-dev sitemap

在您的 nuxt.config 中添加以下代码行

  1. export default defineNuxtConfig({
  2. // ...
  3. nitro: {
  4. prerender: {
  5. routes: [&#39;/sitemap.xml&#39;]
  6. }
  7. }
  8. })

现在,您需要创建一个文件,以在运行 nuxi generate / build 时执行。将该文件命名为 sitemap.xml.ts,放在 /server/routes/ 目录下。

在您刚刚创建的文件中,您需要编写此代码片段 以生成动态路由。

英文:

@nuxtjs/sitemap is no longer compatible with Nuxt3.

For a simple static website

You can quickly generate your sitemap using the new simple-sitemap module for Nuxt3. You can follow their instructions here.

If you have dynamic routes (as /blog/[slug].vue)

You can generate your sitemap.xml using only the sitemapJS plugin.

  1. npm install --save-dev sitemap

Add these lines of code in your nuxt.config

  1. export default defineNuxtConfig({
  2. // ...
  3. nitro: {
  4. prerender: {
  5. routes: [&#39;/sitemap.xml&#39;]
  6. }
  7. }
  8. })

Now you have to create a file to execute when you run the nuxi generate / build. Name that file sitemap.xml.ts inside the directory /server/routes/.

Inside the file, you just created you have to write this code snippet in order to generate the dynamic routes.

huangapple
  • 本文由 发表于 2023年4月11日 12:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982409.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定