Setting global variables based on Node.js CLI arguments - javascript

I created a CLI in node.js. For each command the user passes in command line arguments such as environment and tenant. I want to set variables that can be accessed by any other file in the application.
I am using yargs so each one of my CLI commands takes in arguments and calls a function. Before calling the function I would like to set some global variables that the rest of the js files will use.
I realized I can use environment variables for this since you can set them in the code as well.

Related

Reasoning between environment from express and dotenv

I've been seeing a lot of NodeJS and dotenv tutorials and articles where they define an ENV_MODE=development variable within the config.env file.
But require('express').get('env') already gives us the environment express is set on running. Why not just use the express env variable to check the environment mode? Why do we need to also set the explicit variable in the .env file?
You have to set the environment variable somewhere.
Doing it in a .env file ties it to a specific computer, which means you don't have to either:
Remember to set it manually before you run the program
Bind it into package.json and risk running the wrong command when you run the code on staging/qa/production
… and it lets you keep it with any other environment variables you have (such as API keys) which shouldn't be committed to version control (since they are secrets).

Read environment variables outside of next js

I am using next js as the basis for my application. My environment variables work as long as they are read within the scope of next js but I need to use them inside a script that does not use next js.
With dotenv we can do the following:
require('dotenv').config();
Which parses the environment config files and allows you to get the variables from process.env.
Because I am using next js I would prefer to use their environment variable implementation instead of adding a second way to store environment variables.
Is there an equivalent of require('dotenv').config(); for next js or another way to load the environment variables outside the scope of next js?
Ok I went into the git repo of next js and found a way to do it.
You have to install #next/env and then run require('#next/env').loadEnvConfig('./');

React configure variables based on environment

In my react app, I have a component say chart in which an external API is called.
While running in the local, the API URL will be localhost:8080
When deployed and the API URL should be prod:8080
The examples based on .env uses process.env and inside the component where the API is called process is undefined.
Is there a way to access the env file inside the component which runs on the browser.
If you are using CRA:
React's documentation on environment variables:
"Your project can consume variables declared in your environment as if they were declared locally in your JS files. By default you will have NODE_ENV defined for you, and any other environment variables starting with REACT_APP_."
Otherwise you might use something like dotenv and import it in your application index.tsx
For your specific use case, you also want to have 2 .env files since you want one for development and production. So take a look a this.

Auto detecting available cloud functions to deploy when using gcloud cli

Firebase tools auto detects the available functions and deploy when we issue the below command
firebase deploy --only functions
But, we would like to use gcloud cli as it has more control for providing environment variables/for specifying a vpc connector.
Unfortunately, when using gcloud functions deploy, we need to specify the function name for each function.
Is it possible to get the list of http functions/triggers automatically from the source?
Update : As gcloud cli needs the type of the function (http/event), how to find out the type of the exported function automatically so that i can automate instead of specifying each function details?
If you are working with source code meant to be deployed to Cloud Functions by the Firebase CLI, you can write code to:
Load (require()) the module defined by index.js
Iterate its exports - each export is expected to be a function to deploy
There is nothing existing that will do this for you - you will have to write the code.

Access a system defined environment variable in angular js (grunt)

I am new to angular js . I am using grunt file . So, Here, In java we set environment variables and the use it like System.getEnv("") . Now , I have defined a system environment variable and I want to have that in the angular application code for some operation . How can I access that variable in the angular application code ? Or in the grunt file and then how to use it in the controller? thanks for the help.
Grunt uses Node so you can access environment vars with process.env.[your var name].
Since Angular runs in the client there are no system vars.

Categories