4

Assume that .env file name is building mode dependent. How we can specify it when using the TypeORM CLI? It should be something like:

typeorm-ts-node-esm migration:run/
   -d ./01-Source/DataBase/TypeORM_DataSource.ts/
   --env ".env.local.generated"
Takeshi Tokugawa YD
  • 670
  • 5
  • 40
  • 124

2 Answers2

3

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:

  1. 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.
  2. From ormconfig.env.
  3. 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""
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Use dotenv which makes your work done

npm install dotenv --save

Create a .env file in the root of your project:

S3_BUCKET="YOURS3BUCKET"
SECRET_KEY="YOURSECRETKEYGOESHERE"

Code--

require('dotenv').config()
console.log(process.env) // remove this after you've confirmed it is working
s3.getBucketCors({Bucket: process.env.S3_BUCKET}, function(err, data) {})

Dotenv provides 2 functions--

1.Config-

const result = dotenv.config()

if (result.error) {
  throw result.error
}

console.log(result.parsed)

Optional-

require('dotenv').config({ path: '/custom/path/to/.env' })
require('dotenv').config({ debug: process.env.DEBUG })
require('dotenv').config({ override: true })

2.Parse-

const dotenv = require('dotenv')
const buf = Buffer.from('BASIC=basic')
const config = dotenv.parse(buf) // will return an object
console.log(typeof config, config) // object { BASIC : 'basic' }

Optional-

const dotenv = require('dotenv')
const buf = Buffer.from('hello world')
const opt = { debug: true }
const config = dotenv.parse(buf, opt)
// expect a debug message because the buffer is not in KEY=VAL form
Logic
  • 21
  • 6