v4 can't be imported in node
g012 opened this issue Β· 124 comments
Hello,
I'm not knowledgeable at all in all the module formats / import mess of javascript / node, but I'm using KeystoneJS and I import nanoid in it. Since version 4 (2 days ago), I have this error :
Error [ERR_REQUIRE_ESM]: require() of ES Module /node_modules/nanoid/async/index.js from schema.ts not supported. Instead change the require of index.js in schema.ts to a dynamic import() which is available in all CommonJS modules.
This is how I've always imported nanoid :
import { nanoid } from 'nanoid/async';
Reverting to 3.3.4 works.
I see in the commit comment that you removed CJS support, but as I understand, using this import statement, I'm using the new thingy module stuff, not CJS which uses require instead, and you have "type: module" in your package.json, so I really don't know what's wrong here.
Any idea what I'm doing wrong ? Thanks !
I'm also having this issue
Me too.
This is a feature, not a bug. See the changelog for details of the breaking change in version 4.0 (semantic versioning major increment): https://github.com/ai/nanoid/blob/main/CHANGELOG.md#40
Also see the author's feedback in this previously-closed issue: #364 (comment)
Check your TS config.
Seems like TS convert import
in source TS sources to require
. I use this option in tsconfig.json
: "module": "esnext"
(buy you may need to convert the whole app to TS).
This is a feature, not a bug. See the changelog for details of the breaking change in version 4.0 (semantic versioning major increment): https://github.com/ai/nanoid/blob/main/CHANGELOG.md#40
This doesn't help at all, we saw the comment in the commit on main page of code repo, as stated.
Also see the author's feedback in this previously-closed issue: #364 (comment)
In this one the guy says he was using require, and is told to move to import. As stated, I (we ?) are already using import since long.
Check your TS config.
Seems like TS convert
import
in source TS sources torequire
. I use this option intsconfig.json
:"module": "esnext"
(buy you may need to convert the whole app to TS).
Thanks, I've tried changing module: "commonjs" to module: "esnext", but it made no difference. Could you share your whole tsconfig file ? Here's mine:
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"jsx": "react"
}
}
Maybe you should remove "esModuleInterop": true,
. Try to call tcs file.ts
to be sure that you have import
in the compiled results.
Here is one of my examples https://github.com/hplush/slowreader/blob/main/tsconfig.json
Hello @g012 ,
have you seen this doc?
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
Support for ESM depends heavily on tooling, for example you may have different tsconfig.json settings for your unit tests, your dev builds, and your production release ... depending on whether your codebase is managed with WebPack, Rollup, Vite, Jest, Vitest, etc.
The author of NanoID said that he will kindly continue to offer off-the-shelf compatibility with CommonJS by maintaining v3 in parallel to ESM v4, but I am concerned that noise/signal ratio will increase in this issue tracker due to v4 support queries.
As far as I can tell there are no problems with NanoID's published package.json export map etc. so I think it would make sense to continue such general ESM/CJS conversations in a GitHub discussion instead.
:)
I'm having the same issue in two flavors. first one was described above. second one is:
Details:
/home/circleci/project/node_modules/nanoid/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { randomFillSync } from 'crypto'
^^^^^^
SyntaxError: Cannot use import statement outside a module
> 1 | import { customAlphabet } from 'nanoid';
| ^
2 |
3 | const letters = customAlphabet('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
4 | const alphanumeric = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 5);
in short, I need to turn on an experimental feature in node to enable ESModules import in Jest.
I can't wait for ESModules to become a standard, but right now it's not yet frictionless.
this is my update PR: starwards/starwards#847
I think this decision was made far too early. ESM has just started to be supported by NodeJS. I'm not sure if I want to refactor my whole "production application" for ESM right now.
I just wanted to point that out.
. I'm not sure if I want to refactor my whole "production application" for ESM right now.
"We will support 3.x branch with CommonJS for users who canβt migrate to ESM"
Hello @g012 , have you seen this doc? https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
Support for ESM depends heavily on tooling, for example you may have different tsconfig.json settings for your unit tests, your dev builds, and your production release ... depending on whether your codebase is managed with WebPack, Rollup, Vite, Jest, Vitest, etc.
The author of NanoID said that he will kindly continue to offer off-the-shelf compatibility with CommonJS by maintaining v3 in parallel to ESM v4, but I am concerned that noise/signal ratio will increase in this issue tracker due to v4 support queries.
As far as I can tell there are no problems with NanoID's published package.json export map etc. so I think it would make sense to continue such general ESM/CJS conversations in a GitHub discussion instead.
:)
Hi Daniel,
Yes I think you're right, the Keystone JS toolset converts typescript to CJS, and if I disable that, I get lint warnings for k6 core and some others, so I've decided to stick with branch 3 until everything migrates.
Thanks for the help !
This is a feature, not a bug. See the changelog for details of the breaking change in version 4.0 (semantic versioning major increment): https://github.com/ai/nanoid/blob/main/CHANGELOG.md#40
Really terrible decision. Was gonna use this in place of shortid because apparently this is more secure? But since I literally can't import the latest I'm just gonna keep using shortid. Good luck building a product that people can't use!
... since I literally can't import the latest I'm just gonna keep using shortid.
"We will support 3.x branch with CommonJS for users who canβt migrate to ESM"
Are you planning any security updates for 3.x? Can you clarify what you mean with 'support 3.x branch'?
Are you planning any security updates for 3.x? Can you clarify what you mean with 'support 3.x branch'?
Of course. All fixes will be backported.
Use this version while the author fixes commonjs issue:
npm i -S nanoid@^3.0.0
Whatever works for the maintainer. I am happy the person still maintains the CommonJS version for security updates :)
Theres no harm in supporting commonJS neither does it has any performance ,build or maintainability problems. Its too early to move into ESM imo
Dual ESM/CJS packages is not a good thing. As a developer of dual-publish I can say it without a doubt.
- You need to duplicate every file in npm package which increase
node_modules
size. - It leads to expected problems in some non-standard systems (for instance, Jest is using custom ESM resolver and dual packages could increase the error rate).
- Dual packages requires extra build step (like
dual-publish
orbabel
). With ESM-only you can publish your repository directly. - It also make more complex another tools. For instance, Size Limit will require extra plugin.
- There is no simple way to use version from repository (for instance, to test PR) without some painful bugs. Because dual packages requires build step.
Current state of ecosystem when we need to support both ESM and CJS is very hard for maintainers. Our community need to migrate to ESM-only future. It was already a few years since ESM announce.
Current state of ecosystem when we need to support both ESM and CJS is very hard for maintainers.
That is true, but esm only packages can be very hard for users ;)
Im perfectly happy to keep using v3 until I can switch things to ESM (there is already a pretty long list of other dependencies that cant be updated), but that switch is currently impossible due to electron not supporting ESM in the backend at all, and jest needing nodejs to finish some features before they can have feature parity with CJS.
I personally would have no problem with libraries pushing users to ESM if nodejs & electron were ready for it. But they aren't, so even though ESM has been around for years, the push from library authors feels premature to me.
I personally would have no problem with libraries pushing users to ESM if nodejs & electron were ready for it
Electron and Node.js will not be ready for years if there will not be push from the community.
It is better to support ESM issues in Electron and Node.js.
Im perfectly happy to keep using v3 until I can switch things to ESM
You do not need to always use the latest version of the dependency. You need to use the latest supported version. Both branches 3.x and 4.x are supported. Donβt worry that you are on 3.x branch.
I don't really mind maintainers of packages switching to ESM. It's their project. Only I wish they don't forget their old CommonJS packages when vulnerabilities in dependencies or similar occur. So I was glad to hear this is the case for this super useful package :)
I really need to find out how I can get ESM modules working with Jest. Or is there a better alternative for Jest that has better ESM support? Anyone tried Vitest? Looks like its compatible with Jest's expect
?
Or is there a better alternative for Jest that has better ESM support?
For simple Node.js libraries uvu
is better.
For React projects Vitest is the best option.
You do not need to always use the latest version of the dependency. You need to use the latest supported version. Both branches 3.x and 4.x are supported. Donβt worry that you are on 3.x branch.
Exactly. Im using 4.x in some projects and 3.x in others, depending on whether they are ESM or not. Hopefully 3.x will get updates for a few years, as I expect ESM wont be usable for everyone for at least a couple more years
I really need to find out how I can get ESM modules working with Jest. Or is there a better alternative for Jest that has better ESM support? Anyone tried Vitest? Looks like its compatible with Jest's expect?
I believe that Jest does mostly work, but they don't have feature parity with CJS. jestjs/jest#9430
The main blocker for me is the lack of mock support, which I believe is blocked on the required nodejs features still being experimental.
Use this version while the author fixes commonjs issue:
npm i -S nanoid@^3.0.0
Thank you, it's works for me
Just stay on Nano ID 3 if you have CJS project. We will support Nano ID 3 for a few years.
I delete a very hacky and dangerous advice of crazy wraps to load ESM to CJS.
No, I'm going to use v4 using the 'hacky and dangerous' method because I don't want to worry about being stuck on an obsolete version which will not be supported soon.
I don't want to worry about being stuck on an obsolete version which will not be supported soon.
Nano ID 3 is not obsolete. It is a parallel version. We will support it for a few years.
Just use Nano ID 3.x. There are no rational problems with this version.
If a version of an open source node library has got an end of life announced for it, I consider it already on its way out. It's how I and many other devs out there work, and it's all about minimising technical debt.
That will be the only dependency of over 50 in my package.json I have to worry about pinning, which I've managed to avert worrying about by doing this 'super dangerous hack' (which isn't really that dangerous at all, I have an integration test that covers usage of it and continuously audit my packages).
The problem here isn't whether or not people should stay on v3 or not, it's about understanding the reality of many projects out there that do not have the resources or priorities around upgrading to ESM. ESM isn't a magical solution to dependency security, you can still do plenty of mischief whether your lib is used in CJS or ESM mode.
If a version of an open source node library has got an end of life announced for it
There is no EOL announced for Nano ID 3.x.
For now, I am planning to support it util most of CJS users will use it.
it's about understanding the reality of many projects out there that do not have the resources or priorities around upgrading to ESM
You need to understand the complexity of dual CJS/ESM support. As a developer of dual-publish I found the true reality of how hard it is to support all edge cases just tried to support all of them.
This ESM/CJS nonsense is such a mess... how did they honestly not build interop into the spec, it's just causing pain and suffering for all involved.
I'll look into sticking with 3.x, its a great library and I'm sticking with it either way :)
Personally, I donβt have problems with ESM. I have been struggling to get libraries like got, react-markdown to work in Jest test files. Thatβs my main blocker for the switch. And now with the memory issue in latest Node 16 and/or Jest 28 itβs a struggle to upgrade to newer versions :(
Use this version while the author fixes commonjs issue:
npm i -S nanoid@^3.0.0
struggling to convert to ESM, I get random error, finding on StackOverflow nothing work. End up using
yarn add nanoid@3.3.4
I wish I could find some projects that use expressjs typescript with ESM module. Maybe 4.x just for client-side example React app
Just downgrade the package, it will work fine yarn add nanoid@3.3.4
Hi! Have the same issue. I just change the version nanoid. version 3.0.0 instead of version 4.0.0 in package.json file.
Could you try with this configuration in tsconfig complierOptions?
"target": "es2020", "module": "es2020"
ESM is a disaster in typescript. so many projects have issues like this, eg node-fetch and others.
for plain JS it's possible to convert over, but TS support is really patchy esp when you have modules inside other modules.
just hope nobody is using nano in another package as it will suddenly blow up
Good library, unfortunate release decision.. here I join the v3 gang.
I'm trying to use nanoid with TypeScript on nodejs and getting this error.
const nanoid_1 = require("nanoid");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /opt/xxapi/node_modules/nanoid/index.js from /opt/xxapi/dist/controllers/auth/signup.js not supported.
Instead change the require of index.js in /opt/xxapi/dist/controllers/auth/signup.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/opt/xxapi/dist/controllers/auth/signup.js:25:18)
at Object.<anonymous> (/opt/xxapi/dist/routes/auth.js:12:18)
at Object.<anonymous> (/opt/xxapi/dist/index.js:7:32) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v18.7.0
@bootrino you need to use pure ESM app in TS ("module": "esnext"
) to use Nano ID 4.
If you still convert TS app to CJS, use Nano ID 3.
OK, although I shouldn't need to reconfigure TypeScript to use a library.
@ai would you consider releasing an npm module called something likm nanoid-v3 or nanoid-cjs which tracks v3?
I only ask because I am a regular user of tools like yarn upgrade-interactive.
It's going to constantly tell me that I can upgrade nanoid with the current approach
Yes, I'm one of those people that likes to keep everything at cutting edge
It's going to constantly tell me that I can upgrade nanoid with the current approach
I have the same problem on my CJS projects (like PostCSS). I think we should not try to update always on latest version (or go to ESM if we want the latest solution).
Very strange. Is the only way to fix this downgrading to v3? I will do that for now.
Is the only way to fix this downgrading to v3?
No. The best way is to move project to ESM.
If you use TS it means βtrue ESMβ with "module": "esnext"
.
no the best way is to use a less dogmatic package, esp if you're using TS, until the TS flow is a bit smoother.
for a greenfield JS project, starting with ESM is an option.
@ai Apologies, I don't really know what this means. I add new packages all the time, and this never seems to happen. We are using TS, but we have a project set up in a certain way, so unfortunately, we can't just 'move the project to ESM'. v3 works fine for me for now. Thanks
It's a little painful, every time I need to update packages with npm-check-updates, I have to downgrade nanoid to version 3.X.X. I've already tried to migrate the project I'm working on to ESM, but many packages don't support it yet, too many errors that I don't think I can handle.
thanks got not only me
It's a little painful, every time I need to update packages with npm-check-updates, I have to downgrade nanoid to version 3.X.X. I've already tried to migrate the project I'm working on to ESM, but many packages don't support it yet, too many errors that I don't think I can handle.
With npm-check-updates, you can use -x option to exclude some dependency from the update, instead of updating then downgrading. For example to exclude nanoid:
ncu -u -x nanoid
Attempting to change the module system (compilerOptions.module
) from CommonJS
to ESNext
in tsconfig.json. doesn't work in Node.
It now appears all Node users will have to BOTH manually roll back to v3.3.4
AND begin looking for a replacement for this library as CommonJS has been permanently abandoned in v4
.
Too bad it's so easy to accidentally or silently upgrade nano versions and break your entire build, as I did.
Too bad it's so easy to accidentally or silently upgrade nano versions and break your entire build, as I did.
Luckily its just as easy to rollback to v3 and stick with that
After all the time wasted chasing this, and after taking steps to attempt to warn all our developers that this library (and in our case, ONLY this library) can never be updated, you're right. It was pretty easy typing in a different version number. Of course, difficulty in rolling back was never the complaint.
this is terrible, any alternatives to this lib, not doing such bullshit?
Why do good libraries like husky and this one always have to fuck it up somehow?
Why do good libraries like husky and this one always have to fuck it up somehow?
Because moving to ESM is a global trend in JS ecosystem. It is better to join it.
Are you aware that all packages dependent on your library are forced to adapt (right here, right now)? Isn't it better to add a build task that also generates the common js before publishing to npm?
Isn't it better to add a build task that also generates the common js before publishing to npm?
We have it for 3.x release, and it took a lot of time to maintain and have problem for some edge cases.
this is terrible, any alternatives to this lib, not doing such bullshit? Why do good libraries like husky and this one always have to fuck it up somehow?
agreed, this whole shift is zero benefit if you're typescript anyway and endless pain.
I use cuid on another react native project. I think it might have some similar ESM bs issues tho
https://www.npmjs.com/package/cuid
@ai I find your reasoning for not dual publishing unconvincing.
Export maps are part of the esm transition so your exact same reasoning for pushing esm and forcing the ecosystem to migrate applies there too. They are also far more supported than just plain old esm.
esbuild takes like some 10ms to build twice. And thereβs tons of tools to use node register with that are trivial to use if you somehow are forced to import just esm.
And any analyzer worth its weight will need to support export maps to do so.
Iβve gone the dual publishing route and had no complains from tons of users so far. If you do it properly it supports both without and hiccups across all sorts of tools. And you avoid giant threads like this on your project.
The only downside is node_modules being slightly bigger.
The only downside is node_modules being slightly bigger.
The downside is maintaince from the repository owner.
I do not have enough funding from my open source to spend extra time for both ESM and CJS versions.
Fair. Not critiquing just thought Iβd add perspective as someone doing it. Something like tsup or just a small script that runs esbuild twice typically works and can be added as a pre publish or commit hook.
just hope nobody is using nano in another package as it will suddenly blow up
This isn't true, unless you explicitly specify the dependency version as *
.
Something like tsup or just a small script that runs esbuild twice typically works and can be added as a pre publish or commit hook.
Just run esbuild
will not work.
I created dual-publish
and just check how many edge cases maintainer have to think about (Node, webpack, Node + TS, webpack + ts, esbuild, rollup, Parcel).
Maintaining cost is not when everything is working, but when it goes to edge cases.
And if you keep esm by default but allow to explicitly import from nanoid/cjs
? At least I'd be able to alias in that case. Using nanoid in most cases works ok, but setting up jest to allow for certain packages to use esm is a pita in monorepos. Setting up an alias is much easier.
@bkniffler whatβs even easier is just using nanoid@3
And if you keep esm by default but allow to explicitly import from nanoid/cjs?
Maintaince cost will be again too big (for the feedback I have from the community).
For pnpm users who frequently use pnpm update --latest
, they just added a configuration option for pnpm.updateConfig.ignoreDependencies
in v7.13.0:
pnpm add nanoid@3
// package.json
{
"pnpm": {
"updateConfig": {
"ignoreDependencies": ["nanoid"]
}
}
}
@michaelhays itβs nice that this exists, but this probably ignores 3.x
releases as well. I doubt thereβs much that can go catastrophically wrong in nanoid
, but itβs something one should be aware of.
If you're coming here from React, v18 shipped the useId
hook that might work for you.
The entitlement from some of the assholes in this thread is unbelievable. I'm running into this issue myself and yes, it's causing some minor pain. But it would never occur to me to yell at the maintainer and demand changes. Show some fucking respect people.
The only downside is node_modules being slightly bigger.
The downside is maintaince from the repository owner.
I do not have enough funding from my open source to spend extra time for both ESM and CJS versions.
Would you like me to create a PR with the build script for both ESM and CJS versions?
Would you like me to create a PR with the build script for both ESM and CJS versions?
PR is not help. It will take me a week to then fix all possible issues and then I will spend a day every week answering people with rare Jest edge case.
I already wrote dual-publish
build script (it is not so easy btw) but in some moment I found that it is just impossible to cover all edge cases.
nanoid and express are my favorite packages of all time.
If this arm-chair comment is not useful, please excuse me. Possibly the build process could be simplified to enable cjs and esm. As mentioned here, If nanoid exports a single default, a cjs variant can be generated with sed.
cp nanoid.js nanoid.cjs && sed -i 's/export default/module.exports =/' nanoid.cjs
After form-urlencoded began using this approach, cjs/esm issues went away.
In the previous ticket, you did not want to try this approach because of risks. Since risks are already incurred, maybe the approach could be reconsidered.
same here
same issue
I have the same problem. I want to use the latest version available but it doesn't work :/
downgrading to version 3.3.4 worked for me, if it can help
Understand and respect the author's choice, even if I can downgrade to v3, I probably won't use this product again. Hope to find a good replacement soon.
π©
To tell a joke, the software forces the platform to support his software to solve the problems encountered by users.
I found the author to be a double standard person. I hope that the "nanoid" package will no longer appear in my software, especially Node-based backend projects. At least most of the packages I use are built using TS, and support will gradually increase with platform upgrades. These packages developed using TS make my work happy. nanoid makes me desperate to find a replacement.
So much hate, disrespect and general crying while you can just downgrade 1 version and have (almost) the exact same thing, as the only thing that you'd benefit from v4 is the reduced package size - which is actually a few bytes. There are no security fixes, no additional features that you'll miss. What's the point?
And that's not all! This repository is under MIT license so you can just grab it and have your own version if you rely so much on it. You can apply any security fixes that would come up to v4. And there's more! The package is basically a single 85 LOC file which you could adjust to do whatever you want it to do.
Unbelievable π
Reverting back to 3.* works, however, I always update my packages to the latest version, and don't want to keep this one outdated. You can import the package using dynamic import and it works for version 4.0.0. Put this code in constructor or as part of your app's initialization.
(async () => {
const module = await (eval(`import('nanoid')`) as Promise<any>);
const nanoid = module?.nanoid;
console.log(nanoid && nanoid(10));
})();
In my case, I just needed to generated a random code. So I just did this instead:
const crypto = require('crypto');
function generateRandomCode(length, characterSet) {
let code = '';
for (let i = 0; i < length; i++) {
const randomIndex = crypto.randomBytes(1)[0] % characterSet.length;
code += characterSet[randomIndex];
}
return code;
}
// Example usage:
const characterSet = '349ACEFHKLMNPRTWXY';
const codeLength = 5;
const randomCode = generateRandomCode(codeLength, characterSet);
console.log(randomCode); // prints a random code of length 5
the same problem in sequelize seeder esm script, try to use follow replace it.
import { v4 as uuid } from 'uuid'
const nanoid = (size) => {
return uuid().replace(/-/g, '').slice(0, size)
} βββ
I was using this library for educational purposes to generate random IDs for a simple file-based database. Not anymore, I guess. I will just move to crypto.randomUUID()
which is already built into Node.js.
Use 3.3.4 if you need CommonJS support.
Use 4.0.0 if you need ESM support.
Removed CommonJS support. Nano ID 4 will work only with ESM applications. We will support 3.x branch with CommonJS for users who canβt migrate to ESM.
I was using this library for educational purposes to generate random IDs for a simple file-based database. Not anymore, I guess. I will just move to
crypto.randomUUID()
which is already built into Node.js.
Basically, I did the same as built-in would make more sense since may be a little more supportive.
Dual ESM/CJS packages is not a good thing. As a developer of dual-publish I can say it without a doubt.
- You need to duplicate every file in npm package which increase
node_modules
size.
Since the files are only on the file system, I don't see this as a problem, since when bundling with webpack, rollup etc for a nuxt, next, svelte, ... app only the corresponding module file ( cjs or esm ) is included in the bundle.
- It leads to expected problems in some non-standard systems (for instance, Jest is using custom ESM resolver and dual packages could increase the error rate).
Yes, Jest causes problems more often, but I think you can take that into account before you start backporting changes.
- Dual packages requires extra build step (like
dual-publish
orbabel
). With ESM-only you can publish your repository directly.
But cjs, can be used in both a cjs and esm ecosystem. This is not the case with esm for top level imports.
- It also make more complex another tools. For instance, Size Limit will require extra plugin.
- There is no simple way to use version from repository (for instance, to test PR) without some painful bugs. Because dual packages requires build step.
True
Current state of ecosystem when we need to support both ESM and CJS is very hard for maintainers. Our community need to migrate to ESM-only future. It was already a few years since ESM announce.
You are absolutely right, but I think the step, one should go only if e.g. NodeJs Esm no longer runs in expermintal mode.
Things will only have a chance of moving to ESM when nodejs starts enforcing it (which might as well be eternity), until then there's just not enough pressure to do it. Nobody is going to try and update/replace countless mini-libraries down the depedency chain just because of some developer activism. And that's all this is. Everyone has the right to publish open source libraries that output to only ESM or not, but CJS is here to stay and that's the reality.
To those who really don't want to use v3, you have two options:
- Import v4 using a library that is able to import ESM libraries (yes, they exist)
- If you're not going to spam it in your project, just paste this code https://github.com/ai/nanoid/blob/main/nanoid.js
But cjs, can be used in both a cjs and esm ecosystem. This is not the case with esm for top level imports.
CJS canβt be used as module from URL:
import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'
Also CJS has problems with named exports in some environments.
I ran into the same problem today.
I was also using the node-fetch package whose authors made the same mistake as nanoid: their current major version is ESM-only. Almost two years later since this change and their "old" CJS release is still downloaded 25 times more frequently than their current ESM-only release (you can check NPM stats).
I wish package authors would understand that people can't switch to ESM because this would mean breaking the vast majority of their current dependencies. Switching popular packages to ESM-only will end up causing pain to others for 5 years or so and with the shiny new bleeding-edge version being barely used. So a lose-lose situation.
The final solution to this kind of issue will probably be solved in the Javascript standard which must provide a compatible upgrade path without these showstopper compatibility issues.
This should be front page on the readme because this is wasting a lot of peoples time.
@sugoidesune it is in Install
section of docs
@ai Thanks for choosing to support CommonJS in v3.
I tried converting a monorepo to ESM; builds and runs fine, but Jest blew up. No dice for now.
@ai what did you move to? is there some other testing framework that works with nanoid ?
thats worked for me :
npm uninstall nanoid
npm install nanoid@3.3.4
@dcsan not ai, but want to share this opinion after using different test frameworks,
- for small applications using latest version of node: use node's native test runner, node:test,
for everything else: use ava.use node:test for everything -- recent versions of node:test perform as well at least as well as ava
Really terrible decision. Was gonna use this in place of shortid because apparently this is more secure? But since I literally can't import the latest I'm just gonna keep using shortid. Good luck building a product that people can't use!
@garretthogan you should amend your comment, knowing that v3 will continue to support CommonJS, and v4 was clearly indicated to support only ESM.
Thank you @ai for supporting both!