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.

JavascriptSecurity Boundaries

Code

javascriptsnippet
18 lines
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: true

How 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/promises resolves 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

FAQ

Are there third-party dependencies?
No. The imports resolve to Node.js core modules, which the runtime provides.