An implementation of the Web Crypto API is defined as a global variable.
console.log(crypto === require("node:crypto").webcrypto) //→ trueIt will be readable if we use crypto consistently.
This rule enforces whether to use the global crypto variable or the webcrypto property from the crypto module.
This rule has a string option.
{
"n/prefer-global/crypto": ["error", "always" | "never"]
}"always"(default) ... enforces to use the global variablecryptorather thanrequire("crypto").webcrypto."never"... enforces to userequire("crypto").webcryptorather than the global variablecrypto.
Examples of 👎 incorrect code for this rule:
/*eslint n/prefer-global/crypto: [error]*/
const { webcrypto } = require("crypto")
webcrypto.randomUUID()Examples of 👍 correct code for this rule:
/*eslint n/prefer-global/crypto: [error]*/
crypto.randomUUID()Examples of 👎 incorrect code for the "never" option:
/*eslint n/prefer-global/crypto: [error, never]*/
crypto.randomUUID()Examples of 👍 correct code for the "never" option:
/*eslint n/prefer-global/crypto: [error, never]*/
const { webcrypto } = require("crypto")
webcrypto.randomUUID()