This example shows how to use environment variables with Nullstack.
Start by renaming the .env.example file to .env (recommended to be kept in .gitignore).
Then take a look at it's contents and comments describing what every key become after Nullstack consumes it.
💡 By default Nullstack consumes the .env file with exception of when a
-n
/--name
flag is passed, e.g.nullstack start --name=test
, in this case it searches for a .env.test
Application component is the main code and logs values only available at server brought from .env: NULLSTACK_SERVER_PORT
and COMMON_VARIABLE
.
KeysTable is a stateless component that just illustrates how all keys from .env are or not available to the browser.
AccessCount component brings a more functional use of .env keys featuring a counter to API (server functions) access.
- The API is only accessed passing the right password as stored at .env's
NULLSTACK_SECRETS_PASSWORD
. - It was written as when running the app with the
test
script (e.g.npm run test
)--name=test
will be passed, making Nullstack use the .env.test which setsNULLSTACK_SETTINGS_DISABLE_COUNT
(settings.disableCount
) used to fully disable API access and count at all.
💡🏷️ src/environment.d.ts declares types for .env keys adding autocomplete for them at both
process.env
and context keyssettings
/server
Deploy it now with Vercel or preview on StackBlitz:
Execute nulla create
with npm:
npx nulla create --example environment-variables environment-variables-app