Use crypto-js in Node.js
developYou can use crypto-js in Node.js using ES6 imports for specific modules (recommended for tree-shaking/smaller bundles) or CommonJS require for modular or full library access.
ES6 Import (Specific Modules)
Best for typical API call signing use cases where you only need specific algorithms.
Modular Include (CommonJS)
Import specific modules like aes or sha256 individually.
Full Library Access (CommonJS)
Include the entire library to access all available methods via the CryptoJS object.
// ES6 import
import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';
const message = 'hello';
const nonce = '12345';
const path = '/api';
const privateKey = 'secret';
const hashDigest = sha256(nonce + message);
const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));
// Modular CommonJS
var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
console.log(SHA256("Message"));
// Full Library CommonJS
var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));