fs.access Checks Path Permissions
Probe existence and read or write permission with fs.access and its F_OK, R_OK, and W_OK constants, catching ENOENT for missing paths.
Code
import { writeFile, access, constants } from 'node:fs/promises';
await writeFile('config.json', '{}');
// access resolves when the path passes the requested checks
await access('config.json', constants.F_OK);
console.log('exists: config.json');
// R_OK / W_OK / X_OK test read, write and execute permission
await access('config.json', constants.R_OK | constants.W_OK);
console.log('readable and writable: config.json');
// a missing path rejects with ENOENT instead of throwing synchronously
try {
await access('missing.json', constants.F_OK);
} catch (err) {
console.log('missing.json rejected:', err.code === 'ENOENT');
}Output
Captured from a sandboxed run of this snippet in the build pipeline.
exists: config.json
readable and writable: config.json
missing.json rejected: trueHow it works
Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. The only import source is node:fs/promises, a Node.js built-in, exposing writeFile, access, constants.
What to watch for
node:fs/promisesresolves inside Node.js without any package — it is a core module.
Applying it
The demo output is labeled (exists: config.json, readable and writable: config.json, missing.json rejected:), so every printed line maps back to its originating operation.
Statements that matter
await access('config.json', constants.R_OK | constants.W_OK);
await access('missing.json', constants.F_OK);
await access('config.json', constants.F_OK);
Interactive Tools for This Topic
Unix Permissions CalculatorConvert between octal and symbolic Unix file permissions and explain read/write/execute plus setuid, setgid, and sticky bits.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.
FAQ
- Are there third-party dependencies?
- No. The imports resolve to Node.js core modules, which the runtime provides.
Report a problem with this exampleOpens a pre-filled issue on GitHub.