0

Am trying to create dynamic sitemap for my react site

generate-sitemap.js file

    const fs = require("fs");
const routes = require("./src/routes.js");

const sitemapRoutes = routes.map((route) => {
  return {
    url: route.path,
    changefreq: "weekly", // Adjust this according to your preference
    priority: 0.8, // Adjust this according to your preference
  };
});

const sitemapContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  ${sitemapRoutes
    .map((route) => {
      return `
        <url>
          <loc>${route.url}</loc>
          <changefreq>${route.changefreq}</changefreq>
          <priority>${route.priority}</priority>
        </url>
      `;
    })
    .join("")}
</urlset>`;

fs.writeFileSync("./public/sitemap.xml", sitemapContent);

console.log("Sitemap generated successfully!");

My package.json file

{
  "name": "neramclasses-online-nata-coaching-classroom",
  "version": "0.1.0",
  "private": true,
  "main": "index.js",
  "homepage": ".",
  "dependencies": {
    "@emotion/react": "^11.11.4",
    "@emotion/styled": "^11.11.5",
    "@fontsource/roboto": "^5.0.12",
    "@mui/icons-material": "^5.15.9",
    "@mui/material": "^5.15.15",
    "@tawk.to/tawk-messenger-react": "^2.0.2",
    "@testing-library/jest-dom": "^5.17.0",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "axios": "^1.6.8",
    "bootstrap": "^5.3.2",
    "firebase": "^10.10.0",
    "razorpay": "^2.9.2",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-google-autocomplete": "^2.7.3",
    "react-icons": "^5.0.1",
    "react-router-dom": "^6.22.3",
    "react-router-sitemap": "^1.2.0",
    "react-scripts": "^5.0.1",
    "react-slick": "^0.30.2",
    "react-swipeable-views": "^0.14.0",
    "slick-carousel": "^1.8.1",
    "styled-components": "^6.1.8",
    "validator": "^13.11.0",
    "web-vitals": "^3.5.2"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "generate-sitemap": "node generate-sitemap.js"
  },
  "devDependencies": {
    "@babel/plugin-proposal-private-property-in-object": "^7.16.7"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

when i try to run the generate-sitemap file in Node by node generate-sitemap.js

Am gettin gthis following error

import React from "react";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1178:20)
    at Module._compile (node:internal/modules/cjs/loader:1220:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:121:18)
    at Object.<anonymous> (C:\Users\Haribabu\Documents\Github\neram-new\generate-sitemap.js:2:16)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)

Node.js v18.17.1

Am using latest version of node and the following is my routes.js file where i have all my routes

        import React from "react";
import { HomePage } from "./Pages/Home/Home.js";
import ApplicationPage from "./Pages/ApplicationFresh/ApplicationPage.js";
import CutoffPage from "./Pages/CutoffCalc/Cutoff.js";
import ContactPage from "./Pages/ContactUs/ContactPage.js";
import FaqsPage from "./Pages/Faqs/FaqSection.js";
import PremiumBooks from "./Pages/Books/PremiumBooks.js";
import FreeBooks from "./Pages/Books/FreeBooks.js";
import NotFound from "./Pages/Error/Notfound.js";

const routes = [
  { path: "/", element: <HomePage /> },
  { path: "/NATA_Application_Form_2025", element: <ApplicationPage /> },
  { path: "/Application-form-Nata-Coaching", element: <ApplicationPage /> },
  { path: "/nata-cutoff-calculator", element: <CutoffPage /> },
  { path: "/NATA_Coaching_center_near_me_address", element: <ContactPage /> },
  { path: "/NATA-coaching-centers-nearby", element: <ContactPage /> },
  { path: "/faqs", element: <FaqsPage /> },
  { path: "/Premium_Books", element: <PremiumBooks /> },
  { path: "/NATA_Free_Books", element: <FreeBooks /> },
  {
    path: "/Free-Nata-Class-books-online-registration",
    element: <FreeBooks />,
  },
  // 404
  { path: "*", element: <NotFound /> },
];

export default routes;

I know this error is arising since am trying to execute a React program in Node.js, but it is designed to run in a web browser. There is no DOM in Node.js for you to manipulate. But since am new I don't know how to solve this problem.

3
  • 1
    It seems like you are importing routes.js as a common js file (using require) and not a module file (using import). Instead of dealing with mixing and matching, you can convert your main file (generate-sitemap.js) to a .mjs file (just change the extension) and then replace the two require statements with import statements... That should do it Commented May 12, 2024 at 2:31
  • Also I'm assuming the routes.js has an export statement farther down the file that you just didn't paste here, right? Commented May 12, 2024 at 2:32
  • @byteSlayer I changed the file name to mjs and changed the statement from require to import fs from "fs"; import routes from "./src/routes.js"; after which I ran "node generate-sitemap.mjs" but still getting the same error. Also I updated my question with exports of routes.js Commented May 12, 2024 at 2:40

0

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.