I set my TypeORM config entities path like:
"entities": ["src/entities/**/*.ts"]
This works good when I use ts-node. ts-node src/main.ts
After compile typescripts using tsc
, I got a dist
folder with the compiled application:
However, typeORM still tries to get entities from the src
folder instead of dist
. throwing a lot of unexpectec syntax errors for parsing a TS file instead of a JS. So I change the fallowing string to the entities condiguration:
"entities": ["dist/entities/**/*.js"]
It works with node node dist/main.js
but it does not works with ts-node src/main.ts
How can I configure ormconfig.json
to be able to work with both (node
at dist
folder and ts-node
at src
folder)?
I'd suggest using an ormconfig.js
instead of the JSON version and use an environment variable or similar to switch between the two configs. eg; something like the following stripped down example.
const srcConfig = {
"entities": [
"src/entities/**/*.ts"
],
}
const distConfig = {
"entities": [
"dist/entities/**/*.js"
],
}
module.exports = process.env.TS_NODE ? srcConfig : distConfig;
Note you'll need to set the TS_NODE
env var somewhere; I did notice there's a PR not yet merged that will do it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With