The TypeORM documentation says:
Which configuration file Typeorm uses
Sometimes you may wish to use multiple configurations in different formats. When calling getConnectionOptions()
or trying to use createConnection()
without connection options, Typeorm will try to load configurations in the following order:
- From environment variables. Typeorm will try to load the
.env
file using dotEnv if it exists. Typeorm will use this method if the environment variables TYPEORM_CONNECTION
or TYPEORM_URL
are set.
- From
ormconfig.env
.
- From another
ormconfig.[format]
file, in this order: [js,ts,json]
.
That means, TypeORM supports loading configuration from different sources, including environment variables and various ormconfig files.
However, the built-in support for .env
files in TypeORM is limited to loading only the default .env
file in the root directory of your project.
In your specific case, you want to load a custom .env
file (e.g., .env.local.generated
) instead of the default .env
file, which is not part of TypeORM built-in support for specifying a custom .env
file path.
So you might need to consider dotenv
(npm install dotenv
), that loads environment variables from a .env
file into process.env
.
By preloading dotenv
and specifying the custom .env
file path using the dotenv_config_path environment variable, we can load the environment variables from the custom .env
file before TypeORM starts.
This way, when TypeORM looks for the environment variables, they are already loaded into process.env
, and TypeORM can use them as expected.
NODE_OPTIONS="-r dotenv/config" dotenv_config_path=".env.local.generated" \
typeorm migration:run -d ./01-Source/DataBase/TypeORM_DataSource.ts
On Windows, as I explained here:
cmd /v /c "set "NODE_OPTIONS=-r dotenv/config" && set "dotenv_config_path=.env.local.generated" && typeorm migration:run -d "./01-Source/DataBase/TypeORM_DataSource.ts""