Registry / serialization / fs-constants

fs-constants

JSON →
library1.0.0jsnpmunverified

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.

npm install fs-constants
INSTALL
IMPORT
SIG · FS-CONSTANTS
F
fs-constants
serializationjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

constants
const constants = require('fs-constants')
This package exports a single object containing all `fs` constants using CommonJS. For ESM contexts, bundlers typically handle this CJS import.

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'.
Debug
Known issues
gotchaThe existence of this package stems from a breaking change in Node.js where `require('constants')` was deprecated in favor of `require('fs').constants`. Directly using `require('constants')` in modern Node.js versions will lead to deprecation warnings or errors.
fix
Use `require('fs').constants` in Node.js or `fs-constants` for cross-environment compatibility.
affects: >=v6.3.0 of Node.js (for deprecation), >=v10.0.0 (for removal of global `constants` module without `fs` prefix)
gotchaWhile `fs-constants` aims for browser compatibility, its reliance on `require('constants')` in the browser means it needs a bundler (like Webpack or Rollup) to correctly provide a polyfill or shim for these Node.js-specific constants. Without proper bundling, `constants` might be undefined in a browser environment.
fix
Ensure your build setup (e.g., Webpack, Browserify) correctly bundles and provides shims for Node.js `constants` when targeting the browser.
affects: All versions (browser usage)
Errors
Common errors & fixes
ReferenceError: constants is not defined
Attempting to access `constants` in a browser environment without proper bundling or shim, or trying to use `require('constants')` directly in a Node.js environment where it has been removed.
fix
Ensure your browser build process correctly shims Node.js globals, or use `require('fs-constants')` for cross-environment compatibility. In Node.js, prefer `require('fs').constants` or `fs-constants`.
TypeError: require is not a function
This package is CommonJS-only and is being imported in an ECMAScript Module (ESM) context without a transpiler or bundler handling the interop.
fix
If in an ESM file, either use a bundler that handles CJS imports, or dynamically import with `import('fs-constants').then(module => module.constants)` (though `require` is generally preferred for this CJS package).
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
fs-constants — npm install fs-constants · libregistry