Cannot access environment variables in the server/index.js in Nuxt.js - environment-variables

I'm trying to add environment variables to the nuxt.config.js file in the env property to access the variables in server/index.js file but it gives undefined. According to the env documentation, the variables should work in both client and sever. Not sure if i'm missing something? Thanks in advance.
module.exports = {
env: {
baseUrl: process.env.BASE_URL || 'http://localhost:3000'
}
}

All env variables in Nuxt.js must start with NUXT_ENV.
In vue.js it must start with 'VUE_APP'

In your .env file rename variable like VUE_APP_BASE_URL, and use it with that name.
.env file just allow variables that starts with VUE_APP.
You can read more here.

I know it's quite a late answer but just came across the same issue and thought my findings might help to some future mate in trouble.
It was as simple as include the require('dotenv').config() before any process.env.XXX reference.
Note that you might have better solution if using nuxt 2.13+.

Related

How to use environment variables in CloudFlare Worker in local development environment

I have a CloudFlare Worker where I have environment variables set in the CF Settings..Environment Variables interface. I also have this wrangler.toml
In my worker's index.js I have code reading the variable REGISTRATION_API_URL. If the code is running in a deployed environment then it injects the value from the CF Settings into REGISTRATION_API_URL just fine.
But if I run
wrangler dev
or
wrangler dev --env local
then REGISTRATION_API_URL is undefined.
Originally I expected that the variable would be populated by the CF Settings values, but they aren't. So I tried the two vars setting in the wrangler.toml I show here but no difference. And I have spent a lot of time searching the docs and the greater web.
Are environment variables supported in a local dev environment? Any workarounds that people have come up with? Currently I am looking for undefined and defining the variable with a hard-coded value, but this is not a great answer.
Using wrangler 1.16.0
Thanks.
The docs could be more clear but if you are using the newer module syntax, the variables will not be available as global variables.
Environmental variables with module workers
When deploying a Module Worker, any bindings will not be available as global runtime variables. Instead, they are passed to the handler as a parameter – refer to the FetchEvent documentation for further comparisons and examples .
Here's an example.
export default {
async fetch(request, env, context) {
return new Response(env.MY_VAR);
},
};
KV namespaces are also available in the same object.
Maybe a bit late, but: no I don't think you can
But: you can always use self["YOUR_ENV_VARIABLE"] to get the value and then go from there (unfortunately the docs don't mention that)
Here is what I personally do in my Workers Site project to get the Release version (usually inserted via pipeline/action and then inserted via HtmlRewriter into the index.html):
const releaseVersion = self["RELEASE_VERSION"] || 'unknown'

Can someone explain about script variable "environment"?

what is environment variable and how does it work?
♥image = ♥environment⟦USERPROFILE⟧\Desktop\image.png
Here is an explanation of environment variables on the G1ANT Website: Environment Variables
Basically what ♥environment⟦USERPROFILE⟧ is doing is specifying a path to C:\Users\YOURUSERNAMEHERE (The drive letter could be different, it is C:\ on my computer)
So ♥environment⟦USERPROFILE⟧\Desktop\image.png would be something like C:\Users\YOURUSERNAMEHERE\Desktop\image.png

How to write Travis env variable to file?

I'm trying to write a custom Travis env variable to a file for a simple proof of concept thing that I need. However, I'm having trouble getting this to work.
How would I define this in the travis yaml file if my variable is called VARIABLE_X ?
Thanks!
One way to do this is using linux commands, something like:
printenv | grep VARIABLE > all_env
However I don't know how Travis handles the environment (take a look at their docs, here) but it might not work as easily due to encryption, but it should work since your apps wouldn't function if they didn't have the same level of access. If such a case occurs, modifying a few parameters (maybe TRAVIS_SECURE_ENV_VARS) is worth looking into.
If you solved the problem in another way, consider sharing with the community.
Write the environment variable as usual (Shell - Write variable contents to a file)
Define the following within script:
- echo "$VARIABLE_X" > example.txt

Getting values from a config file or environment variable in meteor

This would be very useful for storing API keys or other sensitive information. From what I understand, you can use config files locally but they won't work on meteor.com, but I heard a rumor that environment variables were soon to be supported, or are already as of a recent release, but I can't find any examples.
Can someone provide an example of how to retrieve a value from an environment variable or some other safe location?
You can actually access the process object to retrieve environment variables in Meteor. In essence, just do the same as in this solution
After some thought, storing them all in a .js file inside an object literal, adding that file to the .gitignore, and checking in a corresponding .js.sample file with dummy or blank values would do the trick.
There's a much better way to handle environment variables. If you come from Ruby on Rails you're used to setting your environment variables in your .ENV file or in your config/application.yml file.
Meteor handles environment variables in a similar way.
Create settings.json file
Inside your server folder in your project, create a file and name it settings.json. Add this file to your gitignore file.
Inside this JSON file, you can save any environment variables you need.
{
"facebookAppId": "6666667527666666",
"facebookAppSecret": "00004b20dd845637777321cd3c750000",
"amazonS3Bucket": "bucket-name"
}
Loading the environment variables
To use these values inside your app during runtime, start Meteor with a --settings option flag.
$ meteor run --settings server/settings.json
Use the values
To use the values, just call the Meteor.settings object.
ServiceConfiguration.configurations.upsert(
{ service: "facebook" },
{
$set: {
appId: Meteor.settings.facebookAppId,
secret: Meteor.settings.facebookAppSecret
}
}
);
That's all there is to it! Keep your settings safe, and do not commit your keys.

How can I define values from outside my script?

I'd like to pass some variables into my .nsi script. Either from the environment or the command line, how do I do this?
I found a section in the documentation that suggests I can use the syntax $%envVarName% to use environment variables in my script, but this doesn't seem to work, when I have
File "/oname=$pluginsdir\inst.msi" "$%VERSION%-Installer-64bit.msi"
I get the error
File: "$%VERSION%-Installer-64bit.msi" -> no files found.
$VERSION is in my environment.
Is there something I'm doing wrong with trying to read environment variables, or some other way of passing values into my script?
$%VERSION% should work if you used set VERSION=1.2.3.4
Or you can create defines: makensis -DVERSION=1.2.3.4 myscript.nsi and File: "${VERSION}-Installer-64bit.msi"

Resources