Check and assert Node.js Permission Model permissions at runtime
npm install has-permissionimport hasPermission, {assertPermission, PermissionError} from 'has-permission';
// Check if file system read is allowed
if (hasPermission('fs.read', '/tmp')) {
console.log('Can read /tmp');
}
// Assert a permission or throw
assertPermission('fs.write', '/var/log');
// Catch permission errors
try {
assertPermission('child');
} catch (error) {
if (error instanceof PermissionError) {
console.log(error.scope); // 'child'
}
}Returns true if the permission is granted, or if the Permission Model is not enabled.
Type: string
The permission scope to check (e.g., 'fs.read', 'fs.write', 'child', 'worker').
Type: string
An optional reference (e.g., a file path or URL).
Throws a PermissionError if the permission is denied. No-op if the Permission Model is not enabled.
Type: string
The permission scope to check.
Type: string
An optional reference.
Error thrown when a required permission is not granted.
Type: string
The permission scope that was denied.
Type: string | undefined
The optional reference.
- Node.js Permission Model - Node.js built-in Permission Model
MIT