Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export CommonJS version #43

Open
ologbonowiwi opened this issue Dec 8, 2023 · 19 comments
Open

Export CommonJS version #43

ologbonowiwi opened this issue Dec 8, 2023 · 19 comments

Comments

@ologbonowiwi
Copy link

The problem to solve

Is your feature request related to a problem you are experiencing that is not a bug? Please describe.

I want to use the project, but the project I'm working on does not support ESModules.

Proposed solution

It'd be nice to have a /commonjs counterpart to the current implementation.

Alternatives

Use something like babel to output a CommonJS version before publishing it

@boutell
Copy link
Member

boutell commented Dec 8, 2023

In any async function in commonjs code in Node.js, you can write:

const { generate, count } = await import('random-words');

We plan to use this method ourselves.

I'm interested to know why and whether your project can't use this technique.

@ologbonowiwi
Copy link
Author

ologbonowiwi commented Dec 8, 2023

It's a legacy project that has +2 years; I'm enhancing the testing coverage, hoping to have more reliability for further changes.

The thing is that this project is running on CommonJS in an older Node version (before top-level await); my wish was to use the project to generate some params for a jest it.each. All tests in Jest must be declared synchronously, so having it inside an async function would not be an option.

I ended up installing @faker-js/faker on the end 😕, but having this one portable to CommonJS would be helpful, and I'm sure it'd enhance the project's usage.

@ologbonowiwi
Copy link
Author

The most significant benefit that I see on this project and that faker does not support is setting the seed. Since for automated testing, it'd be pretty beneficial to have consistent randomness, so the test executed would always be the same (I'm using the generated result to declare the test name)

@boutell
Copy link
Member

boutell commented Dec 8, 2023 via email

@ologbonowiwi
Copy link
Author

ologbonowiwi commented Dec 8, 2023

We're under v18.14.2 here. I'll close this, and when I have some availability, I can fork it and add CommonJS support.

Thank you anyway!

@boutell
Copy link
Member

boutell commented Dec 8, 2023 via email

@AlanGreyjoy
Copy link

Why are we trying to normalize esm on a backend......

@pksunkara
Copy link

pksunkara commented Jan 9, 2024

I am compiling TS to code to CommonJS to run on Node 18 and none of the approaches are working:

import { generate } from 'random-words';
async function slugify() => {
	// ...
	const { generate } = await import('random-words');
	// ...
}

Both of them error out with ERR_REQUIRE_ESM

@ologbonowiwi
Copy link
Author

@pksunkara it's returning this error because since you're transpilling into common js, you don't run it in ESM.

About the second version, the exported code uses await import or it's being changed to other thing?

@pksunkara
Copy link

Ofcourse I understand what the issue is.

All I am trying to say is that because of how tsc is compiling the second version and changing it, even the second version doesn't work.

Basically the conclusion is that this library does not work at all for commonjs backend with typescript.

@boutell
Copy link
Member

boutell commented Jan 9, 2024 via email

@max-degterev
Copy link

For everyone looking for CSJ alternatives: https://www.npmjs.com/package/random-word-slugs

It is the same issue as with all Sindre Sorhus packages: legacy codebases exist. Splitting ecosystem into opposing camps doesn't help anyone. Add a compile step people.

@boutell
Copy link
Member

boutell commented Feb 6, 2024

There's no intention here to split the ecosystem, we just can't support everything and have chosen to offer an ES module. For free, keep in mind. Use what you like, and consider upgrading your tooling to something that can also handle ES modules. Or use something else; that's fine too.

@anton-series
Copy link

I spent a day trying to make this package work with ts-node, Node 20 and TypeScript, but I gave up.

Instead, here's a forked version that supports both CommonJS and ESM: https://www.npmjs.com/package/random-words-commonjs

Changes: anton-series#1

@boutell
Copy link
Member

boutell commented Sep 26, 2024

All right, I see your point.

@boutell boutell reopened this Sep 26, 2024
@boutell
Copy link
Member

boutell commented Sep 26, 2024

At the time I declined to support this, I had not realized the extent to which it was painful to use from commonjs. Since then I've had occasion to actually try that, with other modules, and you're right: it's a bad scene.

This is actually scheduled to change - node 22 will, at some point, start supporting requires of ES modules as long as they have no top level async code - but in the meantime, it makes more sense to have tooling for it.

Would you like to submit your changes as a PR? As long as the update procedure is clear I see the benefit at this point. Sorry for making this contentious.

@pksunkara
Copy link

FWIW, with even with that support in node 22, ts-node still doesn't work. Ref: TypeStrong/ts-node#1007 (comment)

@boutell
Copy link
Member

boutell commented Sep 26, 2024

Yeah, I'm not surprised. I hate having so much tooling required for everything, but it's the way of the world.

@Maelstromeous
Copy link

Adding my +1 to this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants