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

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

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

{
  &quot;name&quot;: &quot;nuxt-app&quot;,
  &quot;private&quot;: true,
  &quot;scripts&quot;: {
    &quot;build&quot;: &quot;nuxt build&quot;,
    &quot;dev&quot;: &quot;nuxt dev&quot;,
    &quot;generate&quot;: &quot;nuxt generate&quot;,
    &quot;preview&quot;: &quot;nuxt preview&quot;,
    &quot;postinstall&quot;: &quot;nuxt prepare&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;nuxt&quot;: &quot;^3.3.3&quot;,
    &quot;nuxt-simple-sitemap&quot;: &quot;^2.4.8&quot;,
    &quot;sitemap&quot;: &quot;^7.1.1&quot;
  },
  &quot;dependencies&quot;: {
    &quot;@nuxtjs/bootstrap-vue&quot;: &quot;^2.0.4&quot;,
    &quot;@nuxtjs/sitemap&quot;: &quot;^2.4.0&quot;,
    &quot;@nuxtjs/tailwindcss&quot;: &quot;^6.1.3&quot;,
    &quot;bootstrap-vue&quot;: &quot;^2.23.1&quot;
  }
}

<!-- end snippet -->

nuxt.config.ts

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

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

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

<!-- 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。

npm install --save-dev sitemap

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

export default defineNuxtConfig({
  // ...
  nitro: {
    prerender: {
      routes: [&#39;/sitemap.xml&#39;]
    }
  }
})

现在,您需要创建一个文件,以在运行 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.

npm install --save-dev sitemap

Add these lines of code in your nuxt.config

export default defineNuxtConfig({
  // ...
  nitro: {
    prerender: {
      routes: [&#39;/sitemap.xml&#39;]
    }
  }
})

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:

确定