-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
doc,crypto: import from 'node:crypto' instead of import('node:crypto') #45884
Comments
This is intentional. Because Node.js can be built without crypto support and only the function-like https://nodejs.org/api/crypto.html#determining-if-crypto-support-is-unavailable |
Is it the standard to be built without crypto support? Shouldnt the example be with the most common use case? |
It is what got consensus in #37594 |
All right I'm gonna close this as it seems not to be a bug |
I wonder if now ESM import throws and therefore we can remove this special use case. @nodejs/tsc Tagging you because probably you have more insight on it. |
Node.js can still be built without crypto support. If you want to right your code in a way that takes this into account, you can't use static imports and need to
Importing |
@aduh95 Nothing, I misunderstood what was the reason in the issue above. |
The use of dynamic imports in |
My opinion here is that we should write example for the majority of users, and eventually a specific example right after for people that don't have @panva Any thought on this? |
Sure, that was my position in the original PR too but at that point this is what we agreed to land. |
It seems weird that the example uses const { createHmac } = await import('node:crypto'); I would think it should look something like this: let createHmac;
try {
({ createHmac } = await import('node:crypto'));
} catch {
console.error('This Node.js was built without crypto support');
} |
Neither are the CJS examples but the point is that the syntax used is try-catcheable. |
Reopening at it seems to have created a discussion. |
Not so particular fan of using this pattern: let xyz
try {
xyz = await promise
} catch (err) {} The problem then is that it ain't so type/ide friendly without having to add extra annotation. + it is now mutable instead of being a i would write it more like: const nodeCrypto = await import('node:crypto').catch(() => {})
const hash = nodeCrypto?.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('hex')
if (hash) { ... }
if (nodeCrypto) { ... } or actually use good old |
Affected URL(s)
https://nodejs.org/api/crypto.html#crypto
Description of the problem
in the
esm
example of the first paragraph there is this import:const { createHmac } = await import('node:crypto');
It seems weird, shouldn't be
import { createHmac } from 'node:crypto':
?If so I can open a PR to fix it
The text was updated successfully, but these errors were encountered: