Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

MikroORM refuses to use the global EntityManager

ErrorValidationError: Using global EntityManager instance methods for context specific actions is disallowed

Short answer

MikroORM no longer allows the global identity map. Every unit of work needs its own EntityManager context. In a web app, register RequestContext.create as middleware before your handlers. For jobs, use fork() or the CreateRequestContext decorator. Only tests should disable the check.

  • Applies to MikroORM (current docs), Node.js 22, Express 4, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the failing call runs. A route handler, a queue worker and a script each need a different fix below.

    grep -rn "orm.em\|RequestContext" src
  2. 2Match what you see, then jump to its fix

    1. 1The error appears in route handlers that use the global orm.emNo request context around the HTTP handlersFix
    2. 2The error comes from code that no web request startsWork outside HTTP: a queue handler, a cron job or a scriptFix
    3. 3Only the test suite failsUnit tests that use the global manager on purposeFix

What does the error look like?

  • ValidationError: Using global EntityManager instance methods for context specific actions is disallowed

Cause 1 of 3

No request context around the HTTP handlers

1Check

The docs say using the global EntityManager without a request context is almost always wrong. They require a middleware that forks it per request.

grep -rn "RequestContext.create" src

2Fix

Register the middleware after body and query parsers and just before the handlers that use the ORM.

app.use(express.json());
app.use((req, res, next) => {
  RequestContext.create(orm.em, next);
});

app.get('/', async (req, res) => {
  res.json(await orm.em.find(Book, {}));
});

3Verify

Request the route again; the query should run.

curl -i http://localhost:3000/

Confirmed by mikro-orm.io

Did this fix it?

Cause 2 of 3

Work outside HTTP: a queue handler, a cron job or a script

1Check

RequestContext only helps inside the request. Background code needs its own context.

grep -rn "@CreateRequestContext\|fork(" src

2Fix

Decorate the top-level method with CreateRequestContext, or take a clean manager with fork().

export class MyService {
  constructor(private readonly orm: MikroORM) {}

  @CreateRequestContext()
  async doSomething() {
    // runs in its own context
  }
}

const em = orm.em.fork();

3Verify

Run the job once and check that it finishes without a validation error.

node dist/jobs/run-once.js

Confirmed by mikro-orm.io

Did this fix it?

Cause 3 of 3

Unit tests that use the global manager on purpose

1Check

The docs mention that the check can be switched off, and that this is handy in unit tests.

grep -rn "allowGlobalContext" src test

2Fix

Set allowGlobalContext in the test configuration, or use the MIKRO_ORM_ALLOW_GLOBAL_CONTEXT environment variable. Keep it off in production.

MikroORM.init({
  // ...
  allowGlobalContext: true,
});

3Verify

Run the tests.

npm test

Confirmed by mikro-orm.io

Did this fix it?

How do I stop it coming back?

  • Put the RequestContext middleware next to the ORM setup so nobody adds a route above it.
  • Do not call a decorated method from another decorated method.
  • Keep allowGlobalContext out of the production config.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

TS1259: module can only be default-imported with esModuleInterop

error TS1259: Module '"./node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag

TS1259 means you wrote a default import for a CommonJS module that TypeScript does not treat as having one.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.