I'm trying to use tsyringe
for dependency injection in my Next.js 14.1.0 app. I'm using the new
App Router. But for some reason it isn't working correctly when doing import 'reflect-metadata'; at the top of my Root Layout..
It's still throwing the following error on some pages:
Uncaught (in promise) Error: tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.
Any ideas where should I import this polyfill? I thought the Root Layout was Next.js 14 entry point but it seems it ain't.
Firstly, it would be great if you could share your code and why you want to use
dependency injection. Probably, there could be better and cleaner way to do what you want to do.However, if you must use
dependency injection, try followingReference - https://github.com/vercel/next.js/discussions/46805#discussioncomment-8261261
You may be facing issues with app router because of the React Server components. By above approach we make sure we are importing the library on client side as well.