Replies: 9 comments 24 replies
-
I'm currently struggling with the same thing with the new App Router structure. As far as I can see, there's no real entrypoint anymore, or at least it doesn't really show in the docs, as adding If in your case you're still using the Pages Router (meaning you have a You can read more about it in here. Still, would be really interested to know how to achieve this but with the new App Router. |
Beta Was this translation helpful? Give feedback.
-
@camezcua12 I ended up requiring
|
Beta Was this translation helpful? Give feedback.
-
I found that this issue can be solved by modifying the webpack config in next.config.js. // src/polyfills.ts
import 'reflect-metadata'; /** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config) => {
const originalEntry = config.entry;
config.entry = async () => {
const entries = await originalEntry();
if (
entries['main.js'] &&
!entries['main.js'].includes('./src/polyfills.ts')
) {
entries['main.js'].unshift('./src/polyfills.ts');
}
return entries;
};
return config;
},
};
module.exports = nextConfig; |
Beta Was this translation helpful? Give feedback.
-
@matomesc // next.config.js
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
instrumentationHook: true
}
} // src/instrumentation.ts
// this is the entrypoint of client and server side
export async function register() {
// import the the sides that you wish
await import('./server-container')
await import('./client-container')
}
// src/client-container
'use client'
import 'reflect-metadata'
import { container } from 'tsyringe'
/*...*/
// src/server-container
import 'reflect-metadata'
import { container } from 'tsyringe'
/*...*/ |
Beta Was this translation helpful? Give feedback.
-
This is how I solved it.
// app/layout.tsx
import 'reflect-metadata';
import ClientLayoutComponent from '@/app/ClientLayoutComponent';
export default function RootLayout({
children
}: {
children: React.ReactNode;
}) {
return (
<html>
<body>
<ClientLayoutComponent />
{children}
</body>
</html>
);
} // app/ClientLayoutComponent.ts
'use client';
import 'reflect-metadata';
export default function ClientLayoutComponent(): null {
return null;
} |
Beta Was this translation helpful? Give feedback.
-
If you have a |
Beta Was this translation helpful? Give feedback.
-
Here's how I've solved it on //app/reflect-metadata-client-side.ts
'use client';
import 'reflect-metadata'; //app/layout.tsx
import './reflect-metadata-client-side';
... //app/container.ts
import 'reflect-metadata';
import { Container } from 'inversify';
import myModule from '#/lib/myFeature/module';
const container = new Container();
container.load(... myModule);
export default container; Tested on
|
Beta Was this translation helpful? Give feedback.
-
I encountered a similar issue with Next.js version 14. After some experimentation, I found a workaround by modifying the tsconfig.json file. Here are the changes I made:
By adding these lines to the compilerOptions section, the application was able to run successfully. This solution was tested with Next.js version 14.1.0 and Inversify version 6.0.2. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? |
Beta Was this translation helpful? Give feedback.
-
Summary
I'm trying to use tsyringe for dependency injection for some services used in my api routes and I have no idea where I'm supposed to
import 'reflect-metadata'
. It's supposed to go in the application entry point but as far as I can tell there is no entry point in nextjs. Any ideas?Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions