Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
createFirebaseRulesIntepreter
✓ import createFirebaseRulesIntepreter from 'firebase-rules-parser';
✗ const createFirebaseRulesIntepreter = require('firebase-rules-parser').createFirebaseRulesIntepreter;
This is the default export for the interpreter factory. The CommonJS `require` pattern for default exports is often incorrect and might return `undefined` or the whole module object.
createFirebaseRulesContext
✓ import { createFirebaseRulesContext } from 'firebase-rules-parser';
✗ import createFirebaseRulesContext from 'firebase-rules-parser/createFirebaseRulesContext';
This is a named export. Incorrectly importing it as a default export or from a deep path is a common mistake.
FirebaseRulesIntepreter
✓ import type { FirebaseRulesIntepreter } from 'firebase-rules-parser';
✗ import { FirebaseRulesIntepreter } from 'firebase-rules-parser';
This is the type for the interpreter instance, not a runtime value. Use `import type` to avoid bundling issues and clearly indicate its type-only nature. Before v2.0.0, it was named `FirebaseRulesIntepreterFacade`.
Demonstrates initializing the Firebase rules parser with a rules string and then using it to check read and write access for different paths and authentication contexts.
import createFirebaseRulesIntepreter, { createFirebaseRulesContext } from 'firebase-rules-parser';
const rulesSource = `
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read: if request.auth.uid == userId;
allow write: if request.auth.uid == userId && resource.data.name is string;
}
match /public/{documentId} {
allow read: if true;
}
}
}
`;
// Create an instance of the interpreter
const rules = createFirebaseRulesIntepreter();
// Load your rules source string
rules.init(rulesSource);
// Create a context object for the access check
const context = createFirebaseRulesContext({
auth: {
uid: 'testUserId123',
email: 'test@example.com'
},
resource: {
id: 'testUserId123',
data: {
name: 'John Doe',
value: 123
}
},
// Define triggers for `exists()` and `get()` calls within rules
onExistsCall: (path) => {
return path === '/databases/DEFAULT/documents/users/testUserId123';
},
onGetCall: (path) => {
if (path === '/databases/DEFAULT/documents/users/testUserId123') {
return {
uid: 'testUserId123',
name: 'Test User'
};
}
return null;
}
});
// Check access for a specific path and operation
const hasReadAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', context);
console.log('Read access:', hasReadAccess.read); // Should be true
const hasWriteAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', context);
console.log('Write access:', hasWriteAccess.write); // Should be true
const noAccessContext = createFirebaseRulesContext({
auth: { uid: 'anotherUser' },
resource: { id: 'testUserId123', data: { name: 'Another Name' } }
});
const noReadAccess = rules.hasAccess('/databases/DEFAULT/documents/users/testUserId123', noAccessContext);
console.log('No read access:', noReadAccess.read); // Should be false or undefined
const publicReadAccess = rules.hasAccess('/databases/DEFAULT/documents/public/someDoc', noAccessContext);
console.log('Public read access:', publicReadAccess.read); // Should be true
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'init')
Attempting to call `init` on an undefined interpreter instance, likely due to incorrect default import syntax for `createFirebaseRulesIntepreter` with CommonJS.
fixUse `const createFirebaseRulesIntepreter = require('firebase-rules-parser').default;` for CommonJS, or preferably use ES module imports `import createFirebaseRulesIntepreter from 'firebase-rules-parser';`. Error: Cannot find module 'antlr4'
The `antlr4` library, a peer dependency, is not installed in the project.
fixInstall `antlr4` as a direct dependency: `npm install antlr4` or `yarn add antlr4`.
Property 'FirebaseRulesIntepreterFacade' does not exist on type 'typeof import("firebase-rules-parser")'.
Attempting to reference the old class name `FirebaseRulesIntepreterFacade` after upgrading to v2.0.0 or later.
fixRename `FirebaseRulesIntepreterFacade` to `FirebaseRulesIntepreter` in your code.
Audit
Dependencies
antlr4requiredCore parsing engine, moved to peer dependency in v1.0.2 to allow consumers to manage its version.