nanoid: nanoid@3(3.3.6) doesn't work with CommonJS
Running npm install nanoid@3 will install v3.3.6
https://www.npmjs.com/package/nanoid/v/3.3.6
v3.3.6 is not listed in the repository tags, so I assume it is v4 and does not support CommonJS. I have confirmed that if I specify 3.3.5 in package.json, it works with CommonJS.
Can you please remove 3.3.6 from npm?
The following is an error when "nanoid":"^3.3.6" is specified
error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("nanoid")' call instead.
  To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '/project/path/package.json'.
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 8
- Comments: 30 (9 by maintainers)
Links to this issue
Commits related to this issue
- deps: replace nanoid with randomId util When linking local-components for development use, our use of nanoid causes the error below, preventing the app from loading. Our options to fix this seem to ... — committed to getflywheel/local-components by nickcernis 9 months ago
- deps: replace nanoid with randomId util When linking local-components for development use, our use of nanoid causes the error below, preventing the app from loading. Our options to fix this seem to ... — committed to getflywheel/local-components by nickcernis 9 months ago
I’m experiencing the same issue here with v3.3.6 (error TS1479). Downgrading to v3.3.5 resolves it.
Looking in the package contents on npm for v3.3.6 we can see that the package.json contains
"type": "module", which should not be expected for a 3.x release?I’ll probably move away from CommonJS instead… Thanks for the quick reply 😉
Got the same issue with v4.0.2 using typescript too.
I’ve had to downgrade to 3.3.5, this mess with different module technology is really annoying
I am having this issue on “^5.0.1”
Ohh, I see 🤔
Yes, I had been trying to convert my project to ESM but gave up when there were too many issues with other dependencies.
I missed the fact I still had
"moduleResolution": "NodeNext"in my tsconfig. After correcting this back to"Node", my build is working with nanoid v3.3.6 now. Thanks@ai I investigated a bit and found a fix. The issue is in the package.json
exports. It seems that therequireandimportconditions should have separate type files for typescript to be able to resolve CJS modules correctly (reference), i.e.:instead of the current version:
Note also the
.d.ctsextension for the types file.It would be a trivial fix, but since the
exportsfield is being generated bydual-publishon module publishing, it’s a bit more involved. I opened a PR on thedual-publishrepo to fix this. Tested locally and on the example from @ToshihitoKon and it works well. Let’s continue the discussion on the PR.The types declaration files need to be split for TypeScript to detect that it is a dual package. See: https://github.com/microsoft/TypeScript/issues/50466#issuecomment-1346916117
See also: https://www.typescriptlang.org/docs/handbook/esm-node.html#packagejson-exports-imports-and-self-referencing
i.e. The file
./index.d.tsshould be cloned to./index.d.ctsand./index.browser.d.ts, and then change:to
Very strange. We can check npm package content by https://www.npmjs.com/package/nanoid/v/3.3.6?activeTab=code and it has all CommonJS files.
I tested 3.3.6 and it works:
@mhamann open new issue for 5.x and explain the problem with more details.
The fix by @myftija was released in Nano ID 3.3.7. Thanks for great investigation and fix.
Same issue here when upgrading to
3.3.6and using the base config that is recommended for commonjs typescript so this will be very common issue.Note this seems to become an issue due to typescript 5.2 beta requiring using
Node16moduleResolution when seeingmoduletoNode16(whereas before it owuld work if you set moduleResolution tonodewhich is no longer possible).https://devblogs.microsoft.com/typescript/announcing-typescript-5-2-beta/#module-and-moduleresolution-must-match-under-recent-node-js-settings
A solution may be to have somthing like
import { nanoid } from 'nanoid/cjs'orimport { nanoid } from 'nanoid/nanoid.cjs'although im not 100% on the specifics of how all the interop works.I believe I have come across the same issue. I already had nanoid 3.3.6 installed and working, but upon upgrading @tsconfig/node18 2.0.1 to 18.2.0 I receive the following:
My tsconfig.json looks like:
I import nanoid like so:
This is a NestJS application.
@tonynechar do not plan to stop support soon
@PPRobitaille Nano ID 4 doesn’t support CommonJS. You need to downgrade to 3.x if your TS compiles to CJS code.
Reproduced in a smaller configuration https://github.com/ToshihitoKon/nanoid-v3-issue
Forgot to report, we are using TypeScript.
nanoid:3.3.5
nanoid:3.3.6