Skip to content
Latchkey

NestJS "Reflect.getMetadata is not a function" / reflect-metadata not imported in CI

Nest relies on the reflect-metadata polyfill to read decorator metadata. If it is never imported before the app loads, Reflect.getMetadata is undefined and DI has no type information.

What this error means

The app or a test fails with "TypeError: Reflect.getMetadata is not a function" or DI silently fails to resolve constructor types. Common when a custom entrypoint or test bootstrap omits the import.

NestJS
TypeError: Reflect.getMetadata is not a function
    at Object.<anonymous> (/app/dist/app.module.js:12:44)

Common causes

reflect-metadata is not imported at the top of the entrypoint

A generated Nest app imports it in main.ts; a custom entrypoint or script that skips this import leaves the metadata API undefined.

A test harness bootstraps without the polyfill

Running code outside the normal main.ts (a script, a custom jest setup) can execute decorated classes before reflect-metadata loads.

How to fix it

Import reflect-metadata first

  1. Add import 'reflect-metadata'; as the very first line of the entrypoint.
  2. For jest, add the import in a setup file loaded via setupFiles.
  3. Rebuild and re-run so the polyfill is present before decorators execute.
main.ts
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';

Load the polyfill in jest setup

Ensure tests that construct decorated classes load reflect-metadata before anything else.

jest config
{ "setupFiles": ["reflect-metadata"] }

How to prevent it

  • Keep import 'reflect-metadata'; as the first import in every entrypoint.
  • Add it to jest setupFiles so tests match runtime behavior.
  • Do not tree-shake or reorder the polyfill import in bundlers.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →