Reference

Troubleshooting

Solutions to common permly problems

Forgot to call sync()

Problem: Permission "posts.create" does not exist error when I call can()

Cause: Permissions and roles are in your config but not yet created in the database. You must call sync() once on startup.

Fix:

import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create", "posts.edit"],
  roles: ["editor"]
});

await perms.sync();
const can = await perms.user(1).can("posts.create"); // ✓

Typos in permission/role names

Problem: TypeScript error like Type '"posts.creat"' is not assignable to type...

Cause: You misspelled a permission or role name, and you're using strict: true.

Fix:

  1. Check the spelling matches your config exactly:
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create"],
  roles: ["admin"],
  strict: true
});
await perms.sync();
const can = await perms.user(1).can("posts.create"); // ✓
  1. If you don't want type checking, set strict: false (but you lose autocomplete):
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create"],
  roles: ["admin"],
  strict: false
});
await perms.sync();
true; // ✓ Runs (but permli will error on unknown permission)

LOCK_TIMEOUT errors

Problem: Error: LOCK_TIMEOUT - Could not acquire lock

Cause: Multiple processes are running sync() at the same time, or a lock wasn't released properly.

Fix:

  1. Ensure only one process calls sync() on startup:
import { perms } from "./permly.js";

// Call sync once at startup
await perms.sync();
true; // ✓ Application can now proceed
  1. Retry if you must call sync() from multiple places:
import { perms } from "./permly.js";

async function syncWithRetry() {
  const maxAttempts = 3;
  for (let i = 0; i < maxAttempts; i++) {
    try {
      return await perms.sync();
    } catch (err: any) {
      if (err?.code === "LOCK_TIMEOUT" && i < maxAttempts - 1) {
        await new Promise(r => setTimeout(r, 100)); // Shorter for testing
      } else {
        throw err;
      }
    }
  }
}
await syncWithRetry(); // ✓
  1. For Galera/MariaDB, ensure consistency:
SET GLOBAL wsrep_sync_wait = 7; -- Ensure reads see all committed data

Galera cluster and GET_LOCK

Problem: Error: Timeout waiting for lock with MariaDB Galera cluster

Cause: Galera doesn't support GET_LOCK() well across nodes. Permly uses it for concurrency safety.

Fix:

  1. Use a single-node MySQL/MariaDB server for permly data
  2. Or configure wsrep consistency:
SET GLOBAL wsrep_sync_wait = 7;
SET SESSION wsrep_causal_reads = 1;
  1. Or consider using PostgreSQL instead (better lock support):
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(), // In production, use postgresAdapter(client)
  permissions: ["posts.create"],
  roles: ["admin"]
});
await perms.sync(); // ✓

Prisma migrate and schema drift

Problem: Error: Column "perm_..." does not exist after prisma migrate

Cause: Prisma reset or migrations cleared the permly tables.

Fix:

  1. Regenerate tables with permly CLI:
npx permly migrate --yes
await perms.sync();
  1. Or restore from backup:
# Restore your database backup
npx permly migrate --yes
await perms.sync();
  1. Prevent this by excluding permly tables from Prisma:

In prisma/schema.prisma, don't include permly tables in your schema. Let permly manage them via the CLI.

esModuleInterop with postgres driver

Problem: require of ES module... not supported when using pg with CommonJS

Cause: The pg driver is ESM-first; CommonJS imports need special handling.

Fix:

Set esModuleInterop: true and allowSyntheticDefaultImports: true in your tsconfig.json to enable CommonJS imports of ESM modules. Alternatively, use await import("pg") in async contexts instead of require().

Missing database driver

Problem: Error: Cannot find module 'mysql2'

Cause: You're using mysqlAdapter() but haven't installed the driver.

Fix:

npm install mysql2   # For MySQL adapter
npm install pg       # For Postgres adapter
npm install mongodb  # For MongoDB adapter
npm install mongoose # For Mongoose (optional, if using it)

isPermission returning false before catalog loads

Problem: isPermission("posts.create") returns false even though it's in my config

Cause: sync() hasn't completed yet; the internal catalog is empty.

Fix:

import { perms } from "./permly.js";

await perms.sync(); // Must call first
const isValid = perms.isPermission("posts.create"); // true ✓

Or check the permission is in your config by using TypeScript's type system. The InferPermission type will reject typos at compile time instead of runtime.

Multiple servers and cache

Problem: User loses permission on Server B shortly after being granted on Server A

Cause: The in-memory cache on each server is independent. Changes on one server don't instantly update others.

Fix:

Disable the in-memory cache when running on multiple servers by setting cache: { enabled: false } in your config, or use a short TTL with eventual consistency. Alternatively, use a shared caching layer (Redis) or accept that permission changes may take time to propagate across all servers.

Permission changes not applying

Problem: await perms.user(1).can("posts.edit") returns false, but I just granted it

Cause: Result is cached. Either cache TTL hasn't expired, or sync() was called and cached the old state.

Fix:

  1. Check cache settings:
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";

const perms = createPermissions({
  adapter: memoryAdapter(),
  permissions: ["posts.create"],
  roles: ["admin"],
  cache: { ttl: 5 } // Short TTL or ttl: 0 for no cache
});
await perms.sync(); // ✓
  1. Or restart your app so the cache is cleared

No receiver for ACTION_VIEW broadcast

Problem: Flutter push notifications aren't working (if using permly in Flutter backend)

Cause: (Platform-specific - see Flutter app debugging)

Fix: See the permly package docs; this is a client app issue, not a backend issue.