I cannot fucking believe that even after all this time it is so hard to import a CJS npm module into a ESM node project

How are these different. I don't understand. All I know is that the libraries did not work until I managed to derive these two magic lines by trial and error. I don't even know whose documentation I was supposed to be looking at to understand what to do here, TypeScript's, Node's, or the ES standard/MDN. Every time I think I've Figured Out JavaScript there is some new baffling wrinkle I had no suspicion could exist.

Screaming: So I am using the json-order library. npmjs.com/package/json-order
It claims inbuilt TypeScript support.

I say:
import orderedJson from 'json-order';

I believe?? this imports the default object from the library. The TS type system now believes orderedJson has a field orderedJson.parse.

It doesn't. orderedJson contains an object named "default", which contains an object named parse. If I say:

import {default as orderedJson} from 'json-order';

That fixes it—but now TS insists it's wrong

Now, I appear to have fixed the problem in a way that BOTH makes TypeScript happy AND produces functioning code, and that is to import the orderedJson object as _orderedJson and then follow it up with this terrifying hack:

// @ts-ignore
const orderedJson:typeof _orderedJson = _orderedJson.default;

And now everything seems correct. However: This is terrifying

Follow

@mcc superstitiously liking in hopes that I will not need to use this 🫣

Sign in to participate in the conversation
CleverLibre Social

CleverLibre Social is an inclusive social instance for open discussion, learning, and community.
All cultures welcome.
Hate speech and harassment strictly forbidden.