Skip to main content
This recipe transforms usage of the deprecated crypto.fips property to use the modern crypto.getFips() and crypto.setFips() methods.

What It Does

The codemod transforms:
  • Reading crypto.fipscrypto.getFips()
  • Writing crypto.fips = valuecrypto.setFips(value)
  • Import statements to include getFips and setFips

Usage

npx codemod nodejs/crypto-fips-to-getFips

Examples

Reading FIPS Mode Status

Before
import crypto from "node:crypto";
import { fips } from "node:crypto";

// Using crypto.fips
crypto.fips;
fips;
After
import crypto from "node:crypto";
import { getFips, setFips } from "node:crypto";

// Using crypto.getFips()
crypto.getFips();
getFips();

Enabling FIPS Mode

Before
import crypto from "node:crypto";
import { fips } from "node:crypto";

// Using crypto.fips = true
crypto.fips = true;
fips = true;
After
import crypto from "node:crypto";
import { getFips, setFips } from "node:crypto";

// Using crypto.setFips(true)
crypto.setFips(true);
setFips(true);

Disabling FIPS Mode

Before
import crypto from "node:crypto";
import { fips } from "node:crypto";

// Using crypto.fips = false
crypto.fips = false;
fips = false;
After
import crypto from "node:crypto";
import { getFips, setFips } from "node:crypto";

// Using crypto.setFips(false)
crypto.setFips(false);
setFips(false);

What is FIPS Mode?

FIPS (Federal Information Processing Standards) mode enables cryptographic operations that comply with FIPS 140-2 requirements. When enabled:
  • Only FIPS-approved cryptographic algorithms are available
  • Node.js must be built with FIPS support
  • Useful for applications requiring government-standard cryptography
FIPS mode can only be enabled at startup or if Node.js was built with FIPS support. Check if FIPS is available using crypto.getFips().

Why Migrate?

The crypto.fips property was deprecated in Node.js v10.0.0. Using property accessors for FIPS mode is not recommended.
Migrating to the getter/setter methods:
  • Provides explicit method calls for better code clarity
  • Aligns with Node.js API conventions
  • Enables better error handling
  • Ensures compatibility with future Node.js versions

Common Usage Patterns

Check and Enable FIPS

import { getFips, setFips } from 'node:crypto';

if (!getFips()) {
  try {
    setFips(true);
    console.log('FIPS mode enabled');
  } catch (err) {
    console.error('Failed to enable FIPS mode:', err);
  }
}

Conditional FIPS Mode

import { setFips } from 'node:crypto';

if (process.env.ENABLE_FIPS === 'true') {
  setFips(true);
}

Deprecation Reference

This migration addresses DEP0093.
  • crypto.getFips() - Returns the current FIPS mode status (boolean)
  • crypto.setFips(bool) - Enables or disables FIPS mode
  • Node.js must be compiled with --openssl-fips for FIPS support

Build docs developers (and LLMs) love