{"id":10922,"library":"fs-constants","title":"fs-constants: Cross-Environment FS Constants","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/mafintosh/fs-constants","tags":["javascript"],"install":[{"cmd":"npm install fs-constants","lang":"bash","label":"npm"},{"cmd":"yarn add fs-constants","lang":"bash","label":"yarn"},{"cmd":"pnpm add fs-constants","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package exports a single object containing all `fs` constants using CommonJS. For ESM contexts, bundlers typically handle this CJS import.","symbol":"constants","correct":"const constants = require('fs-constants')"}],"quickstart":{"code":"const constants = require('fs-constants')\n\n// Example: Accessing common file system constants\nconsole.log('Read Only Flag:', constants.O_RDONLY)\nconsole.log('Write Only Flag:', constants.O_WRONLY)\nconsole.log('Read/Write Flag:', constants.O_RDWR)\nconsole.log('File type: Regular file', constants.S_IFREG)\n\n// These constants are useful for low-level file operations\n// often with Node's 'fs' module methods like fs.openSync or fs.statSync\n// (though fs-constants itself doesn't provide fs functions)\n\n// In a browser environment, this would provide similar constants\n// if a bundler correctly resolves 'constants'.","lang":"javascript","description":"Demonstrates how to import and access the file system constants provided by the package."},"warnings":[{"fix":"Use `require('fs').constants` in Node.js or `fs-constants` for cross-environment compatibility.","message":"The 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.","severity":"gotcha","affected_versions":">=v6.3.0 of Node.js (for deprecation), >=v10.0.0 (for removal of global `constants` module without `fs` prefix)"},{"fix":"Ensure your build setup (e.g., Webpack, Browserify) correctly bundles and provides shims for Node.js `constants` when targeting the browser.","message":"While `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.","severity":"gotcha","affected_versions":"All versions (browser usage)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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`.","cause":"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.","error":"ReferenceError: constants is not defined"},{"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).","cause":"This package is CommonJS-only and is being imported in an ECMAScript Module (ESM) context without a transpiler or bundler handling the interop.","error":"TypeError: require is not a function"}],"ecosystem":"npm"}