-
-
Notifications
You must be signed in to change notification settings - Fork 794
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
nanoid@3(3.3.6) doesn't work with CommonJS #422
Comments
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: ❯ mkdir test
❯ cd test
❯ npm install nanoid@3
❯ echo "console.log(require('nanoid').nanoid())" > index.js
❯ node index.js
NOTSDu0T64EyUDSCZB7a9
|
Thanks for the reply. Here's the node version and package-lock.json
|
Could you reproduce an error in a small repo? |
I'll try. Give me some time. |
Reproduced in a smaller configuration Forgot to report, we are using TypeScript. nanoid:3.3.5
nanoid:3.3.6
|
Got the same issue with v4.0.2 using typescript too. |
@PPRobitaille Nano ID 4 doesn’t support CommonJS. You need to downgrade to 3.x if your TS compiles to CJS code. |
I'll probably move away from CommonJS instead.. Thanks for the quick reply ;) |
Got the same issue with V3.3.6 when using ts-node too |
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 |
It should. 3.x branch is dual CJS+ESM package. We are using ESM by default (this is why we have Note, that 3.3.5 was broken (I forgot to call dual CJS+ESM package convertor during the release) and has no ESM support at all. The previous working release is 3.3.4 (which has Very likely that you have a problem in TS config, which leads to issues with dual CJS+ESM packages (I honestly stopped to believe in creating dual CJS+ESM packages by issues like this, ESM-only is the only stable way for the future). |
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 |
Do you want you send PR to docs adding |
I am sorry, but I am not confident in my English. |
Will there be long term support for the CommonJS nanoid@3 package? @ai |
@tonynechar do not plan to stop support soon |
awesome! thanks @ai |
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. |
Same issue here when upgrading to Note this seems to become an issue due to typescript 5.2 beta requiring using A solution may be to have somthing like |
The types declaration files need to be split for TypeScript to detect that it is a dual package. See: microsoft/TypeScript#50466 (comment)
i.e. The file
to
|
I am having this issue on "^5.0.1" |
@hfhchan-plb I applied those changes in a branch https://github.com/zirkelc/nanoid/tree/fix/442-types However, TypeScript still complains about. Could you try to install |
i ended up switching from commonjs to modules for many more reasons than this and so i went around the problem. |
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 be to: 1. Downgrade from nanoid 5 to 3: ai/nanoid#422 (comment) 2. Refactor to use a dynamic import (potentially infecting all call sites with `await` and refactoring to make parent functions async). 3. Switch from "module": "commonjs" to "module": "ESNext" and assess the impact of that change. 4. Remove nanoid and create a simple randomId function instead. I went with (4). We don't need the full hardware-random IDs that nanoid provides, and we only used nanoid in one component. nanoid error in getflywheel-local after running nps components.link and nps.build, then opening the app: require() of ES Module /Users/nick.cernis/localdev/local-components/node_modules/nanoid/index.js from /Users/nick.cernis/localdev/local-components/dist/index.js not supported. Instead change the require of /Users/nick.cernis/localdev/local-components/node_modules/nanoid/index.js in /Users/nick.cernis/localdev/local-components/dist/index.js to a dynamic import() which is available in all CommonJS modules.
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 be to: 1. Downgrade from nanoid 5 to 3: ai/nanoid#422 (comment) 2. Refactor to use a dynamic import (potentially infecting all call sites with `await` and refactoring to make parent functions async). 3. Switch from "module": "commonjs" to "module": "ESNext" and assess the impact of that change. 4. Remove nanoid and create a simple randomId function instead. I went with (4). We don't need the full hardware-random IDs that nanoid provides, and we only used nanoid in one component. nanoid error in getflywheel-local after running nps components.link and nps.build, then opening the app: require() of ES Module /Users/nick.cernis/localdev/local-components/node_modules/nanoid/index.js from /Users/nick.cernis/localdev/local-components/dist/index.js not supported. Instead change the require of /Users/nick.cernis/localdev/local-components/node_modules/nanoid/index.js in /Users/nick.cernis/localdev/local-components/dist/index.js to a dynamic import() which is available in all CommonJS modules.
Are there any plans to support |
@myftija please send a PR if you know how to fix it. |
@ai I investigated a bit and found a fix. The issue is in the package.json
instead of the current version:
Note also the It would be a trivial fix, but since the |
The fix by @myftija was released in Nano ID 3.3.7. Thanks for great investigation and fix. |
I've had to downgrade to 3.3.5, this mess with different module technology is really annoying |
@ai I tried v3.3.7 and it seems to work with |
@mhamann open new issue for 5.x and explain the problem with more details. |
Running
npm install nanoid@3
will install v3.3.6https://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 specifiedThe text was updated successfully, but these errors were encountered: