Skip to main content
This recipe transforms imports of fs.F_OK, fs.R_OK, fs.W_OK, and fs.X_OK from the root fs module to fs.constants, addressing the deprecation of these constants at the module root level.

What It Does

The codemod transforms:
  • fs.F_OKfs.constants.F_OK
  • fs.R_OKfs.constants.R_OK
  • fs.W_OKfs.constants.W_OK
  • fs.X_OKfs.constants.X_OK

Usage

Examples

Basic Usage

Before
After

Combined Access Modes

Before
After

Multiple Checks

Before
After

Access Mode Constants

F_OK - File Existence

Checks if the file exists (without checking permissions):

R_OK - Read Permission

Checks if the file is readable:

W_OK - Write Permission

Checks if the file is writable:

X_OK - Execute Permission

Checks if the file is executable:

Why Migrate?

Access mode constants at the root fs module level were deprecated to improve API organization and consistency.
Moving to fs.constants:
  • Groups related constants together
  • Aligns with the location of other fs constants
  • Provides better API organization
  • Matches conventions used by other Node.js modules

Combining Access Modes

You can combine multiple access modes using the bitwise OR operator (|):
fs.access() should not be used to check file accessibility before fs.open(). Instead, open the file directly and handle errors. Using access() first introduces a race condition.

Common Patterns

Check Before Read

Verify Multiple Files

Other fs.constants

The fs.constants object also includes:
  • File Open Flags: O_RDONLY, O_WRONLY, O_RDWR, etc.
  • File Copy Flags: COPYFILE_EXCL, COPYFILE_FICLONE, etc.
  • File Type Constants: S_IFMT, S_IFREG, S_IFDIR, etc.

Deprecation Reference

This migration addresses DEP0176.