How to use Digitalocean's app platform component environmental variables? - javascript

I am using Digital Ocean's app platform to host a NodeJS app. I do not understand the documentation for using environmental variables within my NodeJS code. https://www.digitalocean.com/docs/app-platform/how-to/use-environment-variables/#define-build-time-environment-variables when I try to add the environmental variable like I think the documentation wants me to I get a syntax error: mongoose.connect(${_self.DATABASE_URL}, {useNewUrlParser: true});
What is the correct usage?

Step 1:
In your App settings you should find the App-Level Environment Variables.
You could set a variable there like this:
Keys: APP_LEVEL_EXAMPLE_VAR
Values: rafiki
Don´t forget to save.
Now you have an App-Level Environment Variable.
Step 2:
In your app´s project.yml file, you can set a
variable that reads the App-Level variable like this:
environment: {
EXAMPLE_VAR: ${APP_LEVEL_EXAMPLE_VAR}
}
Step 3:
Now you can access the value of APP_LEVEL_EXAMPLE_VAR in your app like this:
const value = process.env.EXAMPLE_VAR

Related

How to pass variables from Azure Pipeline to JavaScript?

I'm attempting to run an Azure Pipeline, with some environment variables defined in the pipeline itself, and I am using the Maven Build task to build my project with Maven to launch various tests. A JS file automatically runs before each test, and I'm supposed to configure these variables in this file, which would make them available to use in the tests themselves. Currently, I have something like this:
# azure-pipelines.yml
trigger:
- none
pool:
vmImage: ubuntu-latest
variables:
- name: user_secret
value: "foo"
steps:
- task: Maven#3
inputs:
mavenPomFile: 'pom.xml'
goals: test
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
javaHomeOption: 'JDKVersion'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
// config.js
function fn(){
return {
secret: user_secret
};
}
The error I get is "user_secret" is not defined
I am being told I can debug it locally because the pipeline is providing the values as environment variables, and that I should be able to validate that locally by setting the variables when I run my code. Unfortunately, I can't figure out how to do it.
Similar to the above example, I also tried to get the variable process.env.user_secret, but this didn't work for me either.
How can I access Azure variables in my code?

npm-config Environment Variables

I saw that best way to store some secret strings is using config package and environment variables. This is how I set it up.
Created a config folder with 2 files (default.json, custom-environment-variables.json)
In default.json I created this:
{
"passPrivateKey": ""
}
In custom-environment-variables.json I created this:
{
"passPrivateKey": "nodeProject_passPrivateKey"
}
After I set the variable in terminal with this command:
npm config set nodeProject_passPrivateKey=randomKey
When I am reading the variable from terminal with command below it works fine and shows the correct value
npm config get nodeProject_passPrivateKey
However in code I have these lines:
if (!config.get("nodeProject_passPrivateKey")) {
console.error("nodeProject_passPrivateKey has not been set");
}
So yeah the problem is this method config.get() is not reading the value and I am getting the error not set from above. I tried doing everything in vs code as admin, and using config.get on "nodeProject_passPrivateKey" and "passPrivateKey" but the method is still not reading any value.
Why not use dotenv?
You create an .env file where you store all your secrets and you access them through
process.env.myvar

next.js environment variables are undefined (Next.js 10.0.5)

I am coding a website with Next.js and I tried to add google Tag Manager.
I followed the tutorial on the Next.js Github example but for some reasons I can't access to my environment variables.
It says my variable is undefined.
I created a file .env.local on my project folder (at the same level as components, node_modules, pages, etc)
In this file I created a variable like this (test purpose) :
NEXT_PUBLIC_DB_HOST=localhost
And on my index page I tried this code :
console.log("test ", process.env.NEXT_PUBLIC_DB_HOST);
But in my console I get a "test undefined".
I tried to put my variable into an .env file instead, without success.
What I am doing wrong ?
This envs just works in Server Side. To access this envs in Client Side, you need declare in the next.config.js
This way:
module.exports = {
reactStrictMode: true,
env: {
BASE_URL: process.env.BASE_URL,
}
}
Create .env (all environments), .env.development (development environment), and .env.production (production environment).
Add the prefix NEXT_PUBLIC to all of your environment variables.
NEXT_PUBLIC_API_URL=http://localhost:3000/
Use with prefix process.env
process.env.NEXT_PUBLIC_API_URL
Stop the server and restart it:
npm run dev
I hope it works.
This solution for latest version of nextJs (above 9)
Restarting the server worked for me.
Edit & save .env.local
Stop the server and restart it, npm run dev
You should get an output on the next line like this:
> klout#0.1.0 dev
> next dev
Loaded env from [path]/.env.local
For those using NextJS +9 and looking for environment variables in the browser, you should use the NEXT_PUBLIC_ prefix. Example:
NEXT_PUBLIC_ANALYTICS_ID=123456789
See documentation for reference.
After spending countless hours on this, I found that there is a tiny little paragraph in both the pre and post nextjs 9.4 documentation:
(Pre-9.4) https://nextjs.org/docs/api-reference/next.config.js/environment-variables (same as this answer)
Next.js will replace process.env.customKey with 'my-value' at build time.
(^9.4) https://nextjs.org/docs/basic-features/environment-variables
In order to keep server-only secrets safe, Next.js replaces process.env.* with the correct values at build time.
Key words being BUILD TIME. This means you must have set these variables when running next build and not (just) at next start to be available for the client side to access these variables.
This is my next.config.js file.
/** #type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
env: {
BASE_URL: process.env.NEXT_PUBLIC_SITE_URL,
},
};
module.exports = nextConfig;
Restart the server and it worked fine. using Nextjs 12.1.0 with typescript
In my case, Im pasting REACT_APP_API_URL instead of NEXT_PUBLIC_API_URL.
Adding with the most recent version of the documentation on this, v12+.
Using the next.config.js file you can specify server and client variables:
module.exports = {
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: 'secret',
secondSecret: process.env.SECOND_SECRET, // Pass through env variables
},
publicRuntimeConfig: {
// Will be available on both server and client
staticFolder: '/static',
},
}
You can still use an env.local file, and pass the variable in to the next.config.js file. For example:
publicRuntimeConfig: {
DB_URL: process.env.DB_URL
}
And then you can access the variable like this:
import getConfig from 'next/config';
const { publicRuntimeConfig } = getConfig();
publicRuntimeConfig.DB_URL;

Access system variable (Environment Variable) while building Angular 2 application

I need to build my Angular application using one system variable.
System Variable
server_url = http://google.com
My Environment.ts file look like
export const environment = {
production: false,
serveUrl: 'http://someurl.com',
mleServerUrl: 'http://someurl2.com',
botServerUrl: 'http://someurl2.com',
DEBUG_LOG: true,
azureAD: true,
mleEnvironment: 'dev',
multtenant: true,
e2eTest: false
};
I need to replace serveUrl value with http://google.com while executing ng build command.
You need a environment.prod.ts (in the same folder as environment.ts) with
export const environment = {
production: true,
serveUrl: 'http://google.com',
};
then you have to do a ng build --prod
See https://github.com/angular/angular-cli/wiki/build
If you want to use system environment variables instead of the standard angular approach here are a few options:
Create a env service that is available through your Angular app; leverages a env.js file which can be modified at deploy time. https://www.jvandemo.com/how-to-use-environment-variables-to-configure-your-angular-application-without-a-rebuild/
Modify your webpack process with a custom angular build which allows you to read system environment variables at build time https://blog.usejournal.com/system-environment-variables-in-angular-1f4a922c7b4c

Meteor: Development only code? [duplicate]

I need to use different accounts provider's configurations when the meteor application runs as Development, Test or Production environment.
Since Meteor 1.3 these flags work out of the box:
Meteor.isDevelopment
Meteor.isProduction
Meteor.isTest
Meteor.isAppTest
On the server:
var inDevelopment = function () {
return process.env.NODE_ENV === "development";
};
var inProduction = function () {
return process.env.NODE_ENV === "production";
};
Meteor sets the environment variable NODE_ENV to "development" when you run meteor. In production, you can set the variable to whatever you want, otherwise it will default to "production".
Update: I created a smart package to allow this to work on the client and server.
mrt add allow-env
Just set permission rules in a server file.
allowEnv({
NODE_ENV: 1
});
You can use Meteor.settings coupled with the --settings option used when running meteor run or meteor deploy.
For example, to run in dev mode, create a JSON file, call it meteorConfigDev.json, and put the following in it:
{
"public" : {
"mode" : "dev"
},
"anotherProperty" : "anotherValue"
}
Run your app using
meteor --settings meteorConfigDev.json
On the server and on the client you can access the "mode" using:
Meteor.settings.public.mode //in this case it will be "dev"
Note that settings in "public" are available on both the server and the client whereas everything else (in this case "anotherProperty") is only available on the server.
You can then have different configuration files for your different environments.
Very easy. I am running my app on five (yes, five!) different environments. I simply use a switch statement on the ROOT_URL as shown below for four different environments. Of course, you can use an if-else if you only have two environments. Works on the server. Just make a new file called startup.js and use the code example below. Cheers!
switch (process.env.ROOT_URL) {
case "http://www.production.com/":
BLOCK OF CODE HERE
break;
case "http://www.staging.com/":
BLOCK OF CODE HERE
break;
case "http://www.development.com/":
BLOCK OF CODE HERE
break;
case "http://localhost:3000/":
BLOCK OF CODE HERE
break;
}
In general, the format for a switch statement in javascript is
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
UPDATE: Note that Meteor now provides Meteor.absoluteUrl(), which is similar to process.env.ROOT_URL with the addition of extra functionality. See docs.
There is an open pull request at github which would allow for that. Comment/Vote for it, so it is more likely to get included!
A really messy way to accomplish this
https://github.com/possibilities/meteor-environment-hooks
note: the interface is OK IMHO, the implementation is messy

Categories