Access denied by the Node.js permission model
Production Risk
The permission model is a security feature; ERR_ACCESS_DENIED indicates a least-privilege violation that should be audited.
Thrown when an operation is blocked by the Node.js Permission Model. Introduced in Node.js 20, the Permission Model allows restricting what file system paths, network access, child processes, and other resources a Node.js process can use. Attempting an operation outside the granted permissions throws ERR_ACCESS_DENIED.
- 1Reading a file path not covered by --allow-fs-read
- 2Writing to a path not covered by --allow-fs-write
- 3Spawning a child process when --allow-child-process is not granted
- 4Accessing the network without --allow-net
Triggered whenever a restricted operation is attempted and the Permission Model is active.
// Run with: node --experimental-permission --allow-fs-read=/tmp app.js
const fs = require('fs');
fs.readFileSync('/etc/passwd'); // not in allowed paths — throws ERR_ACCESS_DENIEDexpected output
Error [ERR_ACCESS_DENIED]: Access to this API has been restricted
Fix 1
Grant the required permission via CLI flags
WHEN When the permission model is active and the operation is legitimate
# Allow reading /etc/passwd explicitly node --experimental-permission \ --allow-fs-read=/tmp \ --allow-fs-read=/etc/passwd \ app.js
Why this works
Adding the specific path to the allow list grants the process access while keeping other paths restricted.
Fix 2
Restructure the app to only access permitted paths
WHEN When adopting the permission model for security hardening
// Store configs in /tmp/app/ instead of /etc/
const config = JSON.parse(fs.readFileSync('/tmp/app/config.json', 'utf8'));Why this works
Moving resources to permitted paths allows the permission model to be more restrictive.
// Run with: node --experimental-permission --allow-fs-read=/tmp app.js
const fs = require('fs');
fs.readFileSync('/etc/passwd'); // not in allowed paths — throws ERR_ACCESS_DENIED // this triggers ERR_ACCESS_DENIEDtry {
// operation that may throw ERR_ACCESS_DENIED
riskyOperation()
} catch (err) {
if (err.code === 'ERR_ACCESS_DENIED') {
console.error('ERR_ACCESS_DENIED:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_access_denied(...args) {
// validate args here
return performOperation(...args)
}✕ Use --allow-fs-read=* to bypass the permission model
A wildcard grant defeats the purpose of the permission model; grant only what is needed.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev