Pre-defined, special purpose system environment variables for Node/NPM - node.js

Are there any special-purpose variables that I can pre-define for Node?
I've noticed that the in Windows, system environment variables are available to Node, like %PATH%, %OS%, and others. You can define your own too, and they are all available via process.env.your_var_name.
In a lot of the documentation I've been reading, there are references to things like PORT, ENV, NODE_PATH, DEBUG, etc.
Are these "special" to Node itself, or are these just variables I can name & define on my own in the OS?

%PATH%, %OS% are system environment variables and not specific to node. You can access them from the command line also.
Other environment variables like PORT, ENV are defined in a .env file to store credentials related to hosting and database.
You can create a .env file in the base directory of your project and store your own environment variables and access it using dotenv package.
More about environment variables:
https://www.freecodecamp.org/news/heres-how-you-can-actually-use-node-environment-variables-8fdf98f53a0a/

Related

module.exports vs environment variables

What is the difference between storing sensitive credentials in a credentials.js file as opposed to an .env file?
With module.exports you can write:
const KEY = require("./credentials.js");
Whereas with .env files you can write:
const KEY = process.env.KEY;
In both cases you accomplish the same goal of making some variable "global" and accessing it globally. Why do people use .env and dotenv instead of just using Node's built in module.exports system?
Environment variables are meant to be defined on the machine (system). The environment variables can be used on multiple application running on the same system.
Exported variables are only limited to the application you are running.
In your case: If you want to deploy your application on 3 servers (Dev, QA, Prod) but on with different credentials, it doesn't make sense to change the credentials every time when deployment. Even if you define credentials for every server it has to be static and if you want to change it, you must do changes in file and deploy the app again to reflect the changes. In case of environment variables, you just have to change it on the targeted system and restart the server, so there is no need to deploy the app again.
It becomes a mess to manage when there are changes in more than one environment variables. Also, environment variables are useful when it comes to integration with other services.
For security in environment variables, you can read this
When storing credentials:
There is risk of exposing credentials to version control system when stored in config file
It doesn't matter where credentials are stored, if the system is compromised
You can use a runtime configuration file. Find it here

Node.js environment Variables VS configuration file

Can someone explain what's benefits of environment variables in Node.js over regular config file?
In my project I have config.js files with DB details, AWS keys, etc. This file is added to .gitignore and never shared on repository, instead there is demo.config.js file with all required parameters filled with fake creditentials, so you can just copy it as config.js and fill it with correct details after fresh install.
This file is "required" in every file when I need credentials in my project and on my development machine this config file is configured with test server details and with actual production server details on production machine.
Lately I read everywhere that everyone should use environment variables to store credentials safely, but I don't see any benefit to doing so in my project.
I'm not saying it's bad and my approach is better, I just want to know what actual benefit (security or otherwise) will I get with environment variables over my setup?
For me it is more like a common standard than anything else. The way how you use config.js is practically the same as using environment variables. But instead of storing the configuration in environment variables, you store it in js file.
The main difference is how you read that config. All mainstream languages I know, will easily allow you to read from environment variables, there is really wide support for it. Reading from config files brings additional complexity as you need to know the structure of that file, how to parse etc. In some languages (maybe node.js) it is probably easy to read from js file, but in others it could be difficult task.
That's why using environment variables is just a common standard and language agnostic. You can even read it in bash scripts etc.
Edit: adding reference to The Twelve-Factor App, the Config section is particularly connected with above question:
https://www.12factor.net/config
One benefits i see when you are using docker for local development and kubernetes or any container orchestration for SIT/UAT etc where config setting is there . In local development we keep all env variable required and move the same on container based system

How to refer different configuration files with on fixed name for different different environments

How do you manage different configuration files for different environments with single file name.
We've come up with some options, File are organized like
config.prod.ini
config.test.ini
config.dev.ini
And on each environment we create a symlink that points to the correct file.
Example:
config.ini -> config.prod.ini (on production environment)
config.ini -> config.test.ini (on test environment)
config.ini -> config.dev.ini (on development environment)
The .py files reads only from config.ini.
Our systems are written in python 3.x
Is there a better way?
Use an environment variable which contains the config file you want to use,
Otherwise you can pass it as a command line argument
Setting the environment variables is dependent on the OS (or hosting service) you are using.
You can access those variables from python using os module os.environ is a dictionary maps all the environment variables of the current process to their values
Also, you can use the python-dotenv module to load environment variables from a file in your project. this allows you to set a unique configuration for each environment running your code

How to add user level environment variables to be used by GUI linux application?

What is the recommended way to add user level environment variables to be used by a GUI application (binary of a wxPython application) in linux (Ubuntu)? I know there are ~/.bashrc, ~/.cshrc, ~/.profile etc for console apps.
Where can I add new paths to existing PATH?
(/bin:/usr/bin/:/usr/X11R6/bin/usr/local/bin)?
How to add settings as new key value pairs? This is meant to be used by a bunch of applications.
PATH and some other variables are stored in /etc/environment
In my opinion, the best place for settings is /etc/.APPNAME/conf or ~/.APPNAME/conf

Storing securely passwords for connection to DB in opensource projects

Cloud9 is a cool service. IF you create a workspace that is public it will mean that everyone who knows your project url and have an account at cloud9 can browse and download your code. This means that if my project have, for example, connectivity to mongodb, everyone will see login and password to connect to mongo (because it will be in some source file).
The only option I can see to store passwords securely (except making project private) is to somehow add them to environment variables, and use process.env.XXXXXX call within the code. This seems to be secure because even if others may browse my code they cannot open terminal and check what environment variables I have defined.
So, is there a way to add my custom environment variable(s) to that they would be accessible via process.env.XXXXXX inside node's code?
You can define environment variables in ~/.profile. Files outside of the workspace directory /home/ubuntu/workspace are not accessible for read only users. You can do e.g.
$ echo "export SECRET=geheim" >> ~/.profile
to define the variable SECRET and then use it through process.env.SECRET from your application. The runners (from the "run" button) and the terminal will evaluate ~/.profile and make the environment variable available to your app.
When running project with cloud9 runners there is Environment popup on the right side of the runner toolbar. You can use it to add environment variables the way you want, but make sure to not add a name to the config since configs with name are automatically saved in .c9/project.settings
Another solution is to create a file in the directory not exposed in readOnly mode. e.g
echo "password" | sudo tee /xxx
you can even edit /xxx file using vi inside cloud9 terminal.
But Of course the best solution is to buy premium subscription, and get more private workspaces:)

Resources