I have a TypeScript Node.js project that uses:
- Experimental decorators (
"experimentalDecorators": true) - Emit decorator metadata (
"emitDecoratorMetadata": true) - ES modules (
"module": "NodeNext"and"moduleResolution": "NodeNext") - TypeORM entities with decorators
My build setup includes:
- A
tsconfig.jsonconfigured with the above compiler options. - A
buildscript that runstsc --build(the TypeScript compiler). - A
startscript that runs something like:tsc-watch --onSuccess "node dist/src/server.js" --onFailure "echo Cannot compile."
When I run npm start, everything works perfectly - the app runs without any errors.
However, when trying to debug the project in VS Code, using ts-node, tsx, or similar approaches, I encounter errors such as unknown .ts file extensions, decorator metadata not loading, or ES module import issues.
I tried several VS Code launch.json configurations, including:
{
"type": "node",
"request": "launch",
"name": "Debug Server",
"runtimeArgs": ["--loader", "ts-node/esm"],
"program": "${workspaceFolder}/src/server.ts",
"sourceMaps": true,
"skipFiles": ["<node_internals>/**"]
}
The server actually starts, but then TypeORM fails with:
No metadata for "X" was found.
This is how the data source is created:
const ds = new DataSource({
...
entities: ['dist/src/entity/**/*.js'],
subscribers: ['dist/src/subscriber/**/*.js']
});
Why might decorator metadata not be loaded correctly when running via ts-node/esm even if the server starts?