Co-authored-by: n8n-cat-bot[bot] <283985454+n8n-cat-bot[bot]@users.noreply.github.com> Co-authored-by: Roman Makowiak <roman.makowiak@n8n.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@n8n/backend-services
Backend services and HTTP error classes that many backend modules share.
Why this package exists
Backend modules live in packages/cli/src/modules/<name> today. Most of them
import a small set of services from the rest of cli. A module cannot move
into its own workspace package while those imports point at @/... paths in
cli.
This package is the home for that shared set. It sits above @n8n/db and
below n8n (cli) in the dependency graph:
flowchart TD
cli["n8n (cli) and backend modules"] --> bs["@n8n/backend-services"]
bs --> db["@n8n/db"]
bs --> bc["@n8n/backend-common"]
db --> bc
@n8n/backend-common holds the foundation that @n8n/db also needs (logger,
license state, module registry, locks). @n8n/backend-services holds business
and infrastructure services that need the persistence layer or that only
cli and the modules consume.
What lives here
EventService: the shared, typed event bus.EventRelay: the base class for a service that listens toEventServiceevents. Its listener map uses the openEventMap.loginIdOf: the login of a service account in anOperationContext, for audit events.UserDeactivationHooks: modules register cleanup steps that run in the transaction that deactivates a user.PasswordUtility: password hashes and checks with bcrypt.ExecutionDataJsonStore: execution data bundles in blob storage, with the bundle types and the read and write errors.DbStorestays incli.UrlService: instance and webhook URLs.CacheService: application cache access.RedisClientService: shared Redis connections and support code.PostHogClient: product analytics events and feature flags from PostHog.PublisherandSubscriber: the scaling-mode pubsub channels. The command payload types (PubSubCommandMap) live in@n8n/decorators, next to@OnPubSubEvent.ProjectScopeService: project-level scope checks. A module callshasScopes(user, projectId, scopes), notuserHasScopesfromcli.userHasScopesstays inclifor the route decorators and for checks on shared credentials and workflows.
The next PRs move ProtectedResourceRegistry, RoleService and the finder
services here, one area at a time.
Register event payloads
EventMap is an open interface. Each event owner augments it with its payload
types. This keeps domain dependencies out of the shared event bus.
import type {} from '@n8n/backend-services';
declare module '@n8n/backend-services' {
interface EventMap {
'example-completed': { id: string };
}
}
Include the augmentation file in the owner's TypeScript project. If another
package uses these events, include the augmentation in the owner's exported
types. The CLI registers its existing maps in src/events/event-map.ts.
Augmentation needs no runtime registration. All consumers import EventService
from @n8n/backend-services and use the same DI token.
Compatibility with cli
The package config mirrors packages/cli, so a file moved from cli compiles,
lints and tests here without edits:
| Concern | Setup | Mirrors |
|---|---|---|
| TypeScript | common.go + backend.go, lib es2023, strictFunctionTypes, strictPropertyInitialization and useUnknownInCatchVariables off |
packages/cli/tsconfig.json |
| Lint | oxlint --type-aware. eslint.config.mjs is the policy twin that code-health reads |
packages/cli/oxlint.config.mts, eslint.config.mjs |
| Tests | Vitest with the decorators config, one fork per file, the same N8N_USER_FOLDER and N8N_ENCRYPTION_KEY setup |
packages/cli/vitest.config.base.ts, test/setup-test-folder.ts |
Two things do not carry over on purpose:
- There is no
@/path alias.clitests load this package fromdist/and other consumers inline it fromsrc/.tscdoes not rewrite an alias indist/, so it resolves in neither. Use relative imports inside the package. cliturns a set of rules down towarnfor its whole tree. This package keeps the shared backend layer as is. Fix such findings when you move a file.
Rules for adding code
- The code must not import from
n8n(cli). If it needs acliseam, define a narrow DI port here and bind it incliat bootstrap. - At least two backend modules must use the code. A service that one module uses belongs to that module.
- Keep the file layout of
cli(errors/,services/). A move withgit mvkeeps the history readable. - Add shared exports to
src/index.ts. Use@n8n/backend-services/authfor auth registries and provider proxies. This entry point does not load the cache, Redis, or database-backed services. Keep internal file paths private. - Keep
oxlint.config.mtsandeslint.config.mjsin step. A ratchet allowlist for a moved TypeORM leak goes into both.