How to make vite recognize Netlify env vars - environment-variables

I'm using a file named .env.local in dev environment, and all env vars work fine!
When I pushed it to Netlify, they aren't located, is there some specific way of setting them?
I also seted them in Netlify.
But, I keep getting this error log:
What can I do?

Related

Getting 500: Internal Server Error after deploying Next.js project in Vercel. Might be related to Environment Variables

I am getting 500 internal server error after deploying a Next.js app to Vercel. The project works in the local machine but isn't working in the URL to which it is deployed to.
I have used environment variables in Vercel, which might be related to the issue.
I added these 4 env variables - NEXTAUTH_URL, NEXTAUTH_SECRET, TWITTER_CLIENT_ID, TWITTER_CLIENT_SECRET.
In my project, I've created a separate file '.env.local' which contains all of my project-related keys and URLs.
At first, this env variable 'NEXTAUTH_URL' was pointing to 'http://localhost:3000/'
NEXTAUTH_URL = http://localhost:3000/
And then, after deploying my app in Vercel, I updated that variable with the deployed URL in my project as well as in Vercel.
NEXTAUTH_URL = https://twitter-clone-seven-coral.vercel.app/
I have also added the above URL in 'Twitter's Developer Portal' in 'OAuth 2.0' in the 'Callback URI/Redirected URL' section:
Before deploying my app in Vercel, the CALLBACK URI/REDIRECT URL was pointing to https://localhost:3000/api/auth/callback/twitter
and WEBSITE URL was pointing to https://test.com
which I then updated after deploying the app initially.
This is the first time I'm working with Environment variables, so I do not have much idea on how to proceed with this error.
Package.json for reference:
Yes, have to set the Environment Variable , tried it with vercel but didnot supported or maybe missed something, but works fine with netlify, just deploy with the environment variable, from your project .env.local files, get the keys and values give also provide the NEXTAUTH_URL properly , then it should run
Had faced same problem and find out solution after a long research
The Trick is you have to set environment variable to vercel or any host platform
how to set environment variable in vercel
how to set environment variable in heroku

Netlify environment variables that were set in the UI are injected as undefined when running a local dev server

I have set some environment variables in the Netlify UI.
See here:
I am trying to use them in my code like this:
console.log("AUTH0_DOMAIN:");
console.log(process.env.AUTH0_DOMAIN);
console.log("AUTH0_CLIENT_ID:");
console.log(process.env.AUTH0_CLIENT_ID);
console.log("AUTH0_AUDIENCE:");
console.log(process.env.AUTH0_AUDIENCE);
When starting up the CLI local dev server using ntl dev it looks like the environment variables are injected:
But they all come through as undefined as shown here in the console:
So what am I doing wrong?
Why are they coming through as undefined?
P.S. I know I should not be using secret keys here because they will be exposed, but I still want to know how to do it for non-secret stuff.
UPDATE: The environment variables are also undefined after deploying live to Netlify. So it's broken on the live version and dev version.
UPDATE 2: Assigning it to a variable, as per below, also doesn't work:
const a_d = process.env.AUTH0_DOMAIN;
console.log(a_d); // This prints undefined
I am building a Vue app.
Turns out all Vue env variables need to be prefixed with VUE_APP_ inside the code and the Netlify UI.
So for example, it becomes const authDomain = process.env.VUE_APP_AUTH0_DOMAIN; in the code and you also have to use VUE_APP_AUTH0_DOMAIN in the Netlify UI.
This solved it for me:
netlify link
The netlify link command will link your local project to Netlify. See docs.
I did not think it was necessary to use netlify link because I already installed the Netlify CLI and was deploying my site automatically with GitHub but apparently it's needed.

How to set up and use Rails environment variables in production server?

I need to set up an environment variable for my rails app. Both in my local machine and in the production server. I read some tutorials on the internet but NONE has given the complete instruction on how to set and use these variable in the actual production server. I use digital ocean and linux server to host my rails app.
I have spent days trying to figure this out, but still haven't found a clear and complete instruction from setting the variables on my local machine -> push it to git repo -> set and use the variables in production server. So, hope somebody can help me here, thanks!
UPDATE:
This is how I currently setup the environment variables in my rails app by using figoro gem:
You can set system-wide environment variables in the /etc/rc.local file (which is executed when the system boots). If your Rails app is the sole user of the Linux system, that is a good place to store credentials such as API keys because there is no risk of including this file in a public Git repository, as it is outside the application directory. The secrets will only be vulnerable if the attacker gains shell access to your Linux server.
Set the environment variables within /etc/rc.local (do not include the <> characters):
export SOME_LOGIN=<username>
export SOME_PASS=<password>
To see the value of an environment variable, use one of the following commands in the Linux shell:
printenv MY_VAR
echo $MY_VAR
To access those environment variables within Rails, use the following syntax:
Inside .rb files or at the rails console
ENV['MY_VAR']
Inside .yml files:
<%= ENV['MY_VAR'] %>
For anyone still having this issue, figaro now has an easy method in setting the production variables in heroku. Just run:
$ figaro heroku:set -e production
ryzalyusoff.
For Unix
You can use LINUX ENV in rails application.
# .env
GITHUB_SECRET_KEY=SECRET
TWITTER_ACCESS_KEY=XXXXXXXXXXXX
# in rails code
puts ENV["TWITTER_ACCESS_KEY"] # => SECRET
Create .env files for local machine and your production server. Export environment variables like this(on server with ssh):
export GITHUB_SECRET_KEY="XXXXXXXXXXXXXXXXXX"
Anyway, storing keys in config - bad idea. Just add .env.example, others keys configs add to .gitignore. Goodluck.
Example with Rails
For Windows
Syntax
SET variable
SET variable=string
SET /A "variable=expression"
SET "variable="
SET /P variable=[promptString]
SET "
Key
variable : A new or existing environment variable name e.g. _num
string : A text string to assign to the variable.
expression : Arithmetic expression
Windows CMD
I believe we should not push a secret file on git.
To ignore such file use gitignore file and push other code on the git.
On the server side just copy the secret file and create a symlink for that file.
You can find demo here http://www.elabs.se/blog/57-handle-secret-credentials-in-ruby-on-rails
You can set your environment variables in production in the same way, you do it for local system. However, there are couple of gems, which make it easier to track and push to production. Have a look at figaro. This will help you in setting up and deployment of env vars.
You can do this with figaro gem
or in rails 4 there is a file named secret.yml in config folder where you can define your environment variables this file is by default in .gitignore file.For production you need to manually copy that file to server for security reason so that your sensitive information is not available to any one
First create your variable like:
MY_ENV_VAR="this is my var"
And then make it global:
export MY_ENV_VAR
You can check if the process succeeded with:
printenv
Or:
echo MY_ENV_VAR

Change dotenv configuration when deploying to Heroku/production?

I have an ajax call that relies on an API key as part of the request URL. I am using dotenv to hide that key. It works fine when I run it locally, but once deployed on Heroku, it gives me back a:
Uncaught TypeError: Cannot read property '________' of null
It looks as though it's returning null back (but it's totally fine in development). I'm wondering if I have to change any of the settings relating to dotenv when pushing up to Heroku / for production?
Thanks!
I am assuming you are referring to the file named .env as dotenv. Every variable in that file that you need on Heroku must be defined in Heroku config vars : https://devcenter.heroku.com/articles/config-vars
The .env file is an easy way to replicate the Heroku config vars locally. That way your code is prepared to read configuration data from config vars. For code running on Heroku you need to set the heroku config vars.

env variables available in rails console but not in application

Never had such problem before with vps that I set up from zero, this one (Ubuntu 12.04, 64bit) was installed by some other developer.
The problem
in .bashrc file i have:
export FACEBOOK_ID=123456789
export FACEBOOK_SECRET=987654321
now in terminal if I type env I see these variables.
if I open rails console and type ENV["FACEBOOK_ID"] or ENV["FACEBOOK_SECRET"] I also can see the apropriate values.
The problem is that I have to use FACEBOOK_ID in the app in a view file and I do it with:
<%= ENV["FACEBOOK_ID"] %>
on local machine this returns the right value, in production on vps it returns nothing.
My idea is that the vps was not set up correctly, I couldnt find apache on it or ngnix, and the app is in var/www/apps/app_name/.
What could be wrong and how can I get this env variables in my template?
update
files available in root directory:
.bash_profile .bashrc .cshrc .zprofile .zshrc
Don't put it in local environment. What to do when you deploy the app? What to do when you want to develop another app on your local machine which use Facebook id as well?
Use Figaro gem. It's built for handling such case, env variables and private data. The env variables can be set in YAML file in app and won't be committed to repo. You won't regret.

Resources