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.
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
- Add
import 'reflect-metadata';as the very first line of the entrypoint. - For jest, add the import in a setup file loaded via setupFiles.
- Rebuild and re-run so the polyfill is present before decorators execute.
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.
{ "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.