esbuild
esbuild copied to clipboard
import issues
throw new Error(`tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.`);
^
Error: tsyringe requires a reflect polyfill. Please add 'import "reflect-metadata"' to the top of your entry point.
at Object.<anonymous> (C:\Users\murat\OneDrive\Belgeler\Gradient\node_modules\tsyringe\dist\cjs\index.js:5:11)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
I get this error, but I'm sure i'm importing it on the very first line on my ts file, like this;
import 'reflect-metadata';
import 'dotenv/config';
import '@sentry/tracing';
import { Logger } from './new-services-with-DI/Logger';
import sentry from '@sentry/node';
import { Client } from 'discord.js';
import { container } from 'tsyringe';
// ... other things
here's my tsup config file (it's an esbuild wrapper),
import { defineConfig } from 'tsup';
export default defineConfig({
entry: [
'src/**/**.ts',
'!src/@types/*',
],
format: ['esm'],
target: 'esnext',
external: ['undici'],
bundle: true,
splitting: true,
silent: true,
clean: true,
outDir: 'dist',
minify: true,
});
Can someone explain why, or maybe suggest a fix?
tsyringe
requires a feature (emitDecoratorMetadata
) that involves type system which esbuild cannot provide.
If you still want to use esbuild to bundle your source codes, you can use plugins to transform your TypeScript sources to with the reflect metadata. There're 2 possible ways:
- Calling the real TypeScript compiler like @rollup/plugin-typescript
- Calling the babel with plugin babel-plugin-transform-typescript-metadata, this plugin works by matching a special code pattern so it needs to be used carefully
But when I run my code with tsx
, is just runs fine, but when i bundle them manually, this happens.
Oh maybe it's the code splitting that is causing the issues?
Because it messes up the import orders