0

I'm working on a React app and I want to create a sitemap for the same. I'm using react-router-sitemap to generate this sitemap and here is my sitemap-builder.js:

require('babel-register');

const router = require('./root.js').default;
const Sitemap = require('../').default;

const filterConfig = {
  isValid: false,
  rules: [
    /\/admin/,
    /\*/,
  ],
};

(new Sitemap(router)
    .filterPaths(filterConfig)
    .build('https://www.ace-up.com')
    .save('./sitemap.xml')
);

But every time I run the file, I get the following error:

ReferenceError: window is not defined
at Object.initialize (/< path >/node_modules/react-ga/src/index.js:55:8)

I was getting a similar error for all my js files also, wherever I could find them but I ended up commenting them out for the time being to make the sitemap work.

What would be the proper way to get the react-router-sitemap to ignore the window problem?

anonn023432
  • 2,940
  • 6
  • 31
  • 63
  • Can you add the contents of root.js? What's in your js folder which you're including on line 4? Finally, how are you running your script? My guess would be you're running this in a node environment, and one of your includes is including the ReactGA package from here: https://github.com/react-ga/react-ga - this package needs to be run in a browser environment, and cannot be run in a node environment – Anuj Apr 10 '17 at 21:52
  • @Anuj I'm running my script from the terminal. I defined the path in my package.json and I just run it using the command npm run build:sitemap. How can I run it in a browser environment? – anonn023432 Apr 10 '17 at 21:58
  • ReactGA cannot be used from the terminal which is what is throwing the error, however you are correct in trying to generate this site map from the terminal - I'm guessing you're including this file in one of the requires on line 3 or 4? Can you show us the contents of those requires? – Anuj Apr 10 '17 at 22:01
  • `const Sitemap = require('../js/').default;` this line also looks like it's importing an entire directory rather than one file - was this intended? – Anuj Apr 10 '17 at 22:02
  • @Anuj sorry that was by mistake. I was trying to restrict it to the js directory. I changed it back to the way it was supposed to be. And I can't add my whole root.js since it's a commercial project. Your point about removing it as a requirement from root makes sense; I'm just wondering if it'll create any issues if add the require back in after creating the site map and then If it tries to auto-update – anonn023432 Apr 10 '17 at 22:06

0 Answers0