I'm trying to configure Knex to use environmental variables to choose the database to use. For whatever reason, it seems very finicky and quick to forget the setting. The first invocation works, and the second does not:
% NODE_ENV=test npm run knex:migrate # No semicolon
> [email protected] knex:migrate
> npm run knex migrate:latest
> [email protected] knex
> ./node_modules/knex/bin/cli.js "migrate:latest"
Using environment: test # Uses the test environment
Already up to date
Add a semicolon between commands and it doesn't work:
% NODE_ENV=test; npm run knex:migrate # Semicolon
> [email protected] knex:migrate
> npm run knex migrate:latest
> [email protected] knex
> ./node_modules/knex/bin/cli.js "migrate:latest"
Using environment: development # Uses the (default) dev environment
Already up to date
And yet...
% echo $NODE_ENV
test
The variable is still set, but Knex is ignoring it. However, if I set NODE_ENV=test
in my .env
file, it sticks. If I try to pass --env test
to the CLI, it never works. Does anyone have insight?
Edit: If I call npx knex migrate:latest --env test
it gets picked up, but it still doesn't pick up the NODE_ENV
values:
% npx knex migrate:latest --env test # This works
Using environment: test
Already up to date
% echo $NODE_ENV
test
% npx knex migrate:latest # This doesn't
Using environment: development
Already up to date
CodePudding user response:
This is not an issue with knex but the way the shell you are using works.
NODE_ENV=test
is only defining a 'local' variable that by default is not forwarded to sub processes.
export NODE_ENV=test
would make it available to all sub-processes
NODE_ENV=test npm run knex:migrate
would run npm run knex:migrate
with NODE_ENV=test