S
S
Sergey Topolov2020-08-22 23:57:42
Windows
Sergey Topolov, 2020-08-22 23:57:42

How to dynamically export variables from .env file to use them when calling commands?

Itchy hands. But it doesn't work.

There is a file with variables .env
I use it in the project through dotenv
The project uses postgres
Also in package.json there are commands to fill the database.

"db:create" : "psql -h localhost -U postgres -f ./src/backend/database/sql/schema.sql",

"db:fill" : "psql -h localhost -U postgres -f ./src/backend/database/sql/fill-db.sql",

"db:drop" : "dropdb --if-exists -h localhost -U postgres -e typoteka",

"db:init" : "node -r ./config ./src/backend/database/db-init.js",

"db:all" : "cross-env COUNT=20 npm-run-all generate db:drop db:create db:init db:fill",


psql terminal is used to execute sql scripts . each time it is called, it requires a password for each connection to the database. well, I will write it once during the development process in the team

"db:all" : "cross-env COUNT=20 PGPASSWORD=password npm-run-all generate db:drop db:create db:init db:fill"

and does not ask. But it's not right.

I want to dynamically load variables from a file. to export them and that they were forwarded at a call db: all or any other command

it would be desirable a cross-platform way. operating system - Windows. without WSL.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
chupasaurus, 2020-08-23
@chupasaurus

.pgpass , however, it can be pre-generated.

A
Anton Shamanov, 2020-08-23
@SilenceOfWinter

operating system - windows

and what prevents the good old batch file from writing?
in general, I would advise you to learn the basics of the same php or vbsscript in a couple of days - they are quite simple and allow you to solve such problems in a couple of minutes.

R
Roman Mirilaczvili, 2020-08-23
@2ord

Not working with node.js, but the dotenv documentation gives an example:

node -r dotenv/config your_script.js

In this script, you can process arguments to run various scripts and use process.env.DB_PASSother secrets.
Among other things, .env files are not intended for production. Instead, you need to use environment variables, which are set differently on Windows.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question