Sat Jul 30 2022
How to fix "To load an ES module, set "type": "module" in the package.json" when installing next-sitemap
Solve this error encountered when running the next-sitemap in postbuild
I wanted to add a sitemap to my website recently and just wanted the quickest way out as I didn't quite fancy grabbing all the blogpost links from my website and mapping over them to collate them to an xml format etc.
This is where I came across pretty cool package called next-sitemap that seemed to cater to my needs.
The instructions seemed easy enough as almost all the content on my website is generated using static site generation.
I added the package, created a basic config file, added next-sitemap to the postbuild scripts so that it can do its job and then pushed the changed to vercel which takes care of the deployment for me.
The build however failed with the below error, which seemed to be a familiar one on the internet
1Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension
So, if you came here looking for the solution to this issue, all you have to do is change your basic config file from
1/** @type {import('next-sitemap').IConfig} */2const config = {3 siteUrl: "https://kishokanth.com/",4 generateRobotsTxt: true,5};6export default config;
to this
1/** @type {import('next-sitemap').IConfig} */2const config = {3 siteUrl: "https://kishokanth.com/",4 generateRobotsTxt: true,5};6module.exports = config;7
Hope this helps!