Vite environment variables - environment-variables

The doc says:
To prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code
How does one use a variable not prefixed with VITE_?

Related

How to use environment variables in Sveltekit 1.0?

I have an .env file which contains two sensitive items and two non-sensitive. Running Sveltekit 1.0 and using Netlify Serverless functions with a db-helper file which has
require('dotenv').config();
const dbName = process.env.MONGODB_DATABASE;
and similarly gets the other variables. However, this crashes with error "cant find module 'dotenv'!
I tried, with same error ..
const dbName = import.meta.env.MONGODB_DATABASE
I tried process.env['MONGODB_DATABASE'] and import.meta.env['MONGODB_DATABASE']. Failed.
I tried prefixing env vars with VITE using both process.env and import.meta.env with and without [' '] wrapper. Failed.
I read that you dont have to explicitly load dotenv as Vite does this. Tried without. Failed.
Has anyone got a solution to this?
Environment variables should be accessed through these modules:
$env/dynamic/private
$env/dynamic/public
$env/static/private
$env/static/public
Public restricts them to those prefixed with PUBLIC_ (which can be configured).
There is some additional documentation for some adapters regarding the loading of these variables, e.g. for the Node adapter, but there is nothing specific for Netlify.
Looking at the Netlify docs, it looks like you have to use its UI/tools or a Netlify config to load variables rather than using an .env file:
With the Netlify CLI, use env:set to update a site environment variable, env:import to import from an updated .env file, and env:unset to delete a site environment variable and all of its contextual values.
I was using
require('dotenv').config()
which worked locally but gave a "cannot find module dotenv" when deployed (to Netlify). I found that in I used "import" instead of "require" then it worked ..
import dotenv from 'dotenv';
dotenv.config();
So thats a solution. But, H.B. correctly pointed out the new sveltekit way env variables should be used. Thanks for that.
PS. The require/import also fails/works for jsonwebtoken so use import * as jwt from 'jsonwebtoken' instead of const jwt = require('jsonwebtoken')

Running Tomcat with different resources for different environments

I have an application-level context.xml with three different databases connections, and my application successfully connects and works fine against those databases. The war file is added to a Tomcat Docker image, and the container runs great.
But, what I really need is the ability to bring up my WAR file with different context.xml files in different environments (Development, QA, and Production). Each environment has its own set of three database connections (i.e. unique URLs/usernames/passwords but the same resource names).
Is there a mechanism in Tomcat where I can pass an environment variable into the Tomcat container at startup, and specify which context file to use? e.g. if I had META-INF/context_dev.xml, META-INF/context_qa.xml, and META-INF/context_prod.xml.
Or, is there some other different kind of mechanism I should be using to have one Docker image that works with three different sets of database resources?
Thanks,
John
In containers and docker as well as kubernetes, ENV variables are the way to go to pass configuration to your container.
You set your tomcat so it also take them into account and use the ENV name in the file.
For tomcat (independtly of having containers or not) an explanation on how to pass env variables is shown there: Tomcat 8 - context.xml use Environment Variable in Datasource
How to pass env variable to your container: How do I pass environment variables to Docker containers?
Then you can pass your env variable in command line with RAW docker or use an .env file. Changing the command like with different values for the ENV variable or just a different .env file to use will do the trick.
I made a solution that will work for me with minimal changes, taking as inspiration the suggestions I received above. Basically, I put ALL the resources for ALL the environments in context.xml, then I named them like such:
<Resource
name="${PRODENV}/mydb1"
XXXXXXX
/>
<Resource
name="${PRODENV}/mydb2"
XXXXXXX
/>
<Resource
name="${QAENV}/mydb1"
XXXXXXX
/>
<Resource
name="${QAENV}/mydb2"
XXXXXXX
/>
Then, when I start the container, I just add -DPRODENV=jdbc or -DQAENV=jdbc to the JAVA_OPTS environment variable. Only the two that I want get loaded, as appropriate. The rest are just never referenced.

Setting and accessing environmental variable used by rundeck

I have Rundeck 2.10.6-1 installed on a Ubuntu 16.04.4 LTS server and would like to set environmental variables just for rundeck. Rundeck itself is functioning normally.
I am able to set the environmental variables in /etc/environment for system-wide availability, but I'd like them to be accessible only by the rundeck session. One of them is SQLCMDPASSWORD, a plain text password, utilized by SQLCMD, so I'd like tighter access. We ended up using this approach for PSQL as well.
I have the rundeck $HOME directory identified as /var/lib/rundeck and the $USER name confirmed as 'rundeck'.
I find that adding environmental variables to .bashrc, .bash_profile, or .profile in that directory are not utilized by 'rundeck' as confirmed with a commandline job executing only 'printenv'. When logged in to the server as 'rundeck' I see the variables.
Am I setting these in right place?
So, no, those variables were not set in the correct place. Here's why...
There were some clues in the answers to this question and this question but neither was sufficient. Applications have their environmental variables set in their profile, and for rundeck that would be
$ more /etc/rundeck/profile
Second clue is from the head of that file which, loosely quoted, indicates
NOTE: DO NOT MODIFY THIS FILE. It will be replaced when the package is upgraded and your changes will not be saved. To override variables in this file, you can instead create a file at: /etc/default/rundeckd
So, I created that default file and set my environmental variables by adding the following content
export SQLCMDUSER=batman
export SQLCMDPASSWORD='secret_bat_password'
Now it works as intended.

Where is the environment variable written?

I am using Yocto, when i run command env, i could see couple of variables have been set. But when I open up /etc/environment, those variable is not in there.
When i set variable in /etc/environment, for example, proxies, it is not reflected to my system.
How can I know where's the environment variable file?
Thank you.
Yocto environment is set by executing oe-init-build-env script. You can add bitbake variables in local.conf file.

What is PYTHONPATH counterpart for Lua?

I like to make some of my own classes available system-wide in Torch7. In python it is enough to give the class files paths to PYTHONPATH environment variable. Is there any equal way to do this for Lua?
The environmental variable you are looking for is LUA_PATH
At start-up, Lua initializes this [package.path] variable with the value of the environment variable LUA_PATH_5_3 or the environment variable LUA_PATH or with a default path defined in luaconf.h, if those environment variables are not defined. Any ";;" in the value of the environment variable is replaced by the default path.
For example, to have Lua look for files in /home/bob/lualibs, you would set LUA_PATH to /home/bob/lualibs;;

Resources