fs-constants: Cross-Environment FS Constants

1.0.0 · active · verified Sun Apr 19

The `fs-constants` package provides a unified and compatible way to access file system constants (like `O_RDONLY`, `S_IFMT`) across both Node.js and browser environments. It transparently handles the historical divergence and deprecation in Node.js, where `require('constants')` was replaced by `require('fs').constants`. For Node.js, it uses `require('fs').constants`, and for browser environments, it leverages `require('constants')`, which bundlers often polyfill or provide. Currently at version 1.0.0, it is a stable, low-maintenance utility designed to abstract away environment-specific constant imports, enabling more portable code for file system operations. Its primary differentiator is resolving this specific Node.js API evolution and ensuring browser compatibility for `fs` flags without complex conditional logic in application code.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to import and access the file system constants provided by the package.

const constants = require('fs-constants')

// Example: Accessing common file system constants
console.log('Read Only Flag:', constants.O_RDONLY)
console.log('Write Only Flag:', constants.O_WRONLY)
console.log('Read/Write Flag:', constants.O_RDWR)
console.log('File type: Regular file', constants.S_IFREG)

// These constants are useful for low-level file operations
// often with Node's 'fs' module methods like fs.openSync or fs.statSync
// (though fs-constants itself doesn't provide fs functions)

// In a browser environment, this would provide similar constants
// if a bundler correctly resolves 'constants'.

view raw JSON →