Search code examples
javascriptnext.jsnode-modulessitemap

issues with added next.config.js for sitemap in nextjs app


so I recently added the following to a next.config.json file for SEO purposes, but now I can't build or start my app because of issues with next-sitemap.

//next.config.js
const withSitemap = require('next-sitemap');

module.exports = withSitemap({
  changefreq: 'weekly', 
  priority: 0.9,
  generateRobotsTxt: true, 
});

here's the file I added, and below is the error I get when trying to run "next build"

> Build error occurred
Error: Cannot find module '/root/node_modules/next-sitemap/dist/cjs/index.js'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1094:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1087:15)
    at resolveExports (node:internal/modules/cjs/loader:571:14)
    at Module._findPath (node:internal/modules/cjs/loader:645:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1058:27)
    at /root/node_modules/next/dist/server/require-hook.js:113:36
    at Module._load (node:internal/modules/cjs/loader:925:27)
    at Module.require (node:internal/modules/cjs/loader:1139:19)
    at require (node:internal/modules/helpers:121:18)
    at Object.<anonymous> (/root/next.config.js:1:21) {
  code: 'MODULE_NOT_FOUND',
  path: '/root/node_modules/next-sitemap/package.json'
}

I have tried uninstalling node_modules and reinstalling everything to fix any issues with the installation of next-sitemap but nothing seems to work. Has anyone else had this problem or something similar and found a fix?


Solution

  • Try do not make any changes on next.config.js Make changes on next-sitemap.config.js instead

    paste this code to the next-sitemap.config.js

    module.exports = {
      changefreq: 'weekly', 
      priority: 0.9,
      generateRobotsTxt: true, 
    };