question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow es modules to be imported in `next-sitemap.js`.

See original GitHub issue

Is your feature request related to a problem? Please describe. Right now it is not possible to import es modules in next-sitemap.js.

For example: paths.js

export const somePath = 'some/path'

next-sitemap.js

import { somePath } from './paths'

or

const { somePath } = require('./paths')

Both of these won’t work. In other words, it is in no way possible to import an es module into next-sitemap.js.

I could imagine that many next.js users have a paths.js file containing all of their app’s paths. E.g. one would import these into next-sitemap.js to exclude a path.

Describe the solution you’d like There are multiple solutions:

  1. import next-sitemap.js (inside next-sitemap src code) using the import statement as opposed to using the require statement. This would effectively make next-sitemap.js an es module being able to import other es modules. Without using Babel it is necessary though to rename next-sitemap.js to next-sitemap.mjs to use the import syntax. Or adding { type: 'module' } to package.json.
  2. let Babel process next-sitemap.js before executing it using the @babel-node package for example.

Describe alternatives you’ve considered The only alternative I see right now is to hardcode any values you’d like to import and thereby duplicating them. But if the values change and one forgets to update the hardcoded values in next-sitemap.js, things will obviously break…

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:11
  • Comments:12

github_iconTop GitHub Comments

4reactions
JarFrankcommented, Jun 20, 2022

Hi all! unfortunatly the issue still exists.

  • Next: 12.1.5
  • next-sitemap: 3.1.3v

I’m getting this error message:

Loaded env from C:\nextjs\.env.local
✨ [next-sitemap] Loading next-sitemap config: file:///C:/nextjs/next-sitemap.config.js
(node:5768) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\nextjs\next-sitemap.config.js:9
export default config;
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:168:29)
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
2reactions
KishokanthJeganathancommented, Jul 30, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Bypass ES Modules Errors in Next.js with Dynamic ...
Dynamic imports is a feature of Next.js that allows you to work with JavaScript modules conveniently in the browser.
Read more >
ERR_REQUIRE_ESM and Package.json error on Next.js. ...
Support for using ESM-only node_modules is still fine. The reason "type": "module" will break the next server is that it's still written in...
Read more >
next-sitemap
Start using next-sitemap in your project by running `npm i next-sitemap`. ... Define the sitemapSize property in next-sitemap.config.js to ...
Read more >
"module" in the package.json or use the .mjs extension 2022
The warning To load an ES module, set “type”: “module” in the package.json or use the .mjs extension occurs when using ES6 Module...
Read more >
How to transpile ES modules with webpack and Node.js
Note that for the import keyword, we can make a reference to the smallestNumber function we exported from the ESM module or package, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found