Trying to build a module to map through my React router and return the routes. currently trying to test it in Node and getting this error:
Error: [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/corey/projects/react-build-sitemap/src/mapJson.js' imported from /home/corey/projects/react-build-sitemap/index.js Did you mean to import ../src/mapJson.js?
Not really sure how I should be importing this to make it work while testing. This will ultimately get imported into a React project.
import fs from "fs";
import PropTypes from "prop-types";
import { warn } from "console";
import mapJson from "./src/mapJson";
import BasicRouter from "./BasicRouter";
const buildSitemap = (fileName, buildPath, url) => {
const sitemapElements = [
'<?xml version="1.0" encoding="UTF-8?">',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
];
//if component does not exist, throw warning and skip.
if (fileName === undefined || fileName === null) {
throw new warn("Component does not exist to generate sitemap. Skipping.");
}
//read through jsx
const jsxTree = babelParser.parse(fileName, {
sourceType: "module",
plugins: ["jsx"],
});
//find the 'router', 'browserrouter', or 'switch' element.
const router = mapJson(jsxTree);
//if the above elements exist, map through all routes.
if (router !== undefined) {
router.forEach((item) => {
console.log(item);
});
}
//if does not exist, throw a warning saying it doesn't exist and skip running.
//generate xml file string.
//write sitemap.xml file to build path.
};
buildSitemap.propTypes = {
fileName: PropTypes.string,
buildPath: PropTypes.string,
url: PropTypes.url,
};
buildSitemap(BasicRouter, "./src/", "https://coreylanier.com");
export default buildSitemap;
package.json includes "type": "module" and the relatively imported files have ES6 exports as well.
EDIT: here is the ./src/mapJson.js:
const mapJson = (json) => {
//run code
};
export default mapJson;