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.

Unable to use ESM import

See original GitHub issue

I’m seeing errors when importing the module into an ESM project:

import { kml } from '@tmcw/togeojson';
         ^^^
SyntaxError: Named export 'kml' not found. The requested module '@tmcw/togeojson' is a CommonJS module, which may not support all module.exports as named exports.

But if I use this method:

import toGeoJSON from '@tmcw/togeojson';

I see this error:

export { gpx, gpxGen, kml, kmlGen, kmlWithFolders, tcx, tcxGen };
^^^^^^

SyntaxError: Unexpected token 'export'

So I’m kinda stuck. Any idea what’s happening here? Using v5.1.2

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tmcwcommented, May 22, 2022

v5.1.3 released with a fix. Changing file endings for the export from .js to .mjs enables Node.js native ESM support without changing everything else. Thanks for the bug report!

0reactions
neavecommented, May 20, 2022

The fix seems to be to also add "type": "module" to togeojson’s package.json

Yes that could fix this issue, but based on https://github.com/placemark/togeojson/issues/63#issuecomment-1061252314 it may not be that simple:

Okay, it was the type: "module" field, which opens a real can of worms. Introduced 4.6.1, which renames main to .cjs and adds an export field in package.json to direct users to that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to import from libs using ESM · Issue #109 · remix-run ...
The following error is thrown when trying to import from libs using ESM (like unist-util-visit): Error [ERR_REQUIRE_ESM]: Must use import to load ES...
Read more >
Unable to import ESM .ts module in node - Stack Overflow
It seems you are looking to use ESM with Node and TS. tsconfig.json (tested with TypeScript 4.7 and 4.8). On your tsconfig.json file...
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
You can solve the "[ERR_REQUIRE_ESM]: require() of ES Module not supported" by doing one of two things: Use ESM - use import foo...
Read more >
JavaScript modules - MDN Web Docs
Use of native JavaScript modules is dependent on the import and export ... They need to be top-level items; you can't use export...
Read more >
How to Bypass ES Modules Errors in Next.js with Dynamic ...
The error in the image below shows that we can't use the CommonJS approach to import a module. serialize-1. This new standard and...
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