can'f find the "process.env.NODE_ENV" - javascript

The variable process.env.NODE_ENV always bothers me every time I see it in a config file, especially webpack.config.js. Where can I find this? Because sometimes I want to change it's value to production instead of development.

You can set any environmental variable like NODE_ENV while running your applications.
Linux/Mac
NODE_ENV=production node myapp.js
Windows/Powershell
$env:NODE_ENV="production" ; node myapp.js
You can access environmental variable NODE_ENV inside your myapp.js by
process.env.NODE_ENV

process.env.NODE_ENV is Node.js environment.
With the help of this mostly we decide our deployment environment in which we are deploying the code.
It can be development,stag or production.
We can do other stuff on env, it's up to us.
to set in Linux
export NODE_ENV=production
on Windows
set NODE_ENV=production
Example
config.js
{development:{
//your `env` value
},
production:{
//Your `env` value
}}
Now on the basis of NODE_ENV your env will be set

Related

If I set env vars using dotenv and PM2 ecosystem.config.js, which one will Node use?

I assume PM2 appends env vars the 'native' system way at startup, something like:
MYVAR=hey; node app.js
The difference with the dotenv npm package is it MUST append vars another way, because it works inside the script (it can't do MYVAR=someothervar; node app.js because the program is already started), so it works like this:
dotenv.config() //reads .env file and appends stuff to process.env at runtime
Now say PM2 launches MYVAR=hey; node app.js and then inside app.js we run dotenv.config() that reads an .env file containing MYVAR=foo. Which var will be in process.env?
ecosystem.config.js
{
//...standard pm2 config above
env: {
MYVAR: 'ecosystem',
},
}
.env/dotenv
MYVAR=dotenv
Code
dotenv.config()
console.log(process.env.MYVAR)
dotenv.config() will not overwrite variables if it sees they already exist in the process.env (that they've been assigned the PM2 MYVAR=foo; node app.js way.
So process envs set before launch will take precedence.
This is actually in the README of dotenv.
What happens to environment variables that were already set?
We will never modify any environment variables that have already been set. In particular, if there is a variable in your .env file which collides with one that already exists in your environment, then that variable will be skipped. This behavior allows you to override all .env configurations with a machine-specific environment, although it is not recommended.
https://www.npmjs.com/package/dotenv#what-happens-to-environment-variables-that-were-already-set
If you absolutely need to override existing env vars - use the dotenv-override package.

How to send environment variables to Jest CLI?

I have a script that runs a Jest test for me, and I want to set an environment variable before running it, something like this:
my_test_script.sh
IMPORTANT_ENV_VAR=value_that_matters
./node_modules/.bin/jest --useStderr ./__tests__/my.test.js
However, inside "my.test.js" the IMPORTANT_ENV_VAR is not set when I run my test like this.
How do I pass the environment variable into the Jest CLI ?
What i do to solve the problem is ugly, but works.
I'm searching for a better solution (Simply set a Environment Variable by cli), not able to find it.
i create a js file that set the environment variable and i load that file passing it to the setupFile cli command. for example
jest --setupFile=setter.js
and setter.js contains
process.env.A = "B"
so, before running each test, i can read the process.env.A variable.
Use cross-env:
{
"scripts": {
"build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
},
"devDependencies": {
"cross-env": "7.0.3",
}
}
Its purpose is exactly to pass environment variables to an npm script in a cross-platform way.
Going off of #Andrea Bisello's answer, you can load the environment variables from a plain JavaScript file. This is the key take-away.
So, in my environment, I already had a jest.config.ts. Since I was already using a .env file for defining my environment variables (located at the root of my project), I just needed a way to load them when running jest from a CLI at the root of my project.
Doing that is very simple; just add this to the jest.config.ts or jest.config.js:
// jest.config.{ts,js}
const { config } = require('dotenv');
config();
Then, from the root of your repo, you can do something like:
npx jest
Because it's hard to set environment variables in a platform-independent way under npm/package.json, and because jest --setupFiles, while in the usage, isn't in the docs and doesn't seem to work (24.9), jest REALLY DOES need a --envVar option to support environmental overrides external to package.json (like dev/production).
Whoops!
Not a Jest problem, I just needed to export in the shell script :
export IMPORTANT_ENV_VAR=value_that_matters
./node_modules/.bin/jest --useStderr ./__tests__/my.test.js
If you are using npm, you can add this to your package.json to define environment variables in the testing env.
"jest": {
"globals": {
"ENVVAR1": true,
"ENVVAR2": "value"
}
}

How does Node set its environment variable?

When running a Node application, how does Node know what environment it is running in?
I understand that the environment is defined within process.env.NODE_ENV, but how and where is that variable defined?
There multiple ways of setting node variables but most common
1. is to start your console with them enabled as following:
> NODE_ENV=prod node start.js
process.env.NODE_ENV // prod
But there are times when you can explicitly set the env before the start of the file:
process.env.NODE_ENV = 'test';
require('config') // then it will return me the test.json config
// I use this technique mostly for unit tests
2. export the envars in the package.json
"scripts": {
"start": "export NODE_ENV=dev && node server.js", // for linux
"start": "set NODE_ENV=dev && node server", // for windows
"test": "mocha"
},
When you run npm start the script will run the server in dev mode
3. use a npm package as dotenv and setup a .env file
Plugins for env management as dotenv the most common used one. Where you can create .env files with needed ENV variables
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
These usually are set in package.json commands or come from a .env file.
For example, NODE_ENV=development from a .env file can be accessed in process.env.NODE_ENV.
For loading .env files into process.env check out dotenv.
The variable isn't defined unless you define it.
I guess you are using a regular Windows machine.
On Windows you can simply do this.
create a random index file and put this in it: console.log(process.env.NODE_ENV)
and then run it with: set NODE_ENV=productionEnvironment && node index

Webpack environment variable NODE_ENV vs cli params --env

what's the difference between set environment variable and cli environment options?
for example
cross-env NODE_ENV=production webpack
then
const isProd = process.env.NODE_ENV === 'production'
or
webpack --env.production
then
module.exports = function (env) {
....
}
which is better?
webpack --env.production is prob better since it will ensure that all variables that should be set are set. Admittedly, it is probably only setting NODE_ENV=production - however, in the future things might change and this approach will ensure you are always taking the best current approach.

How do you detect the environment in an express.js app?

How do you detect what environment an expressJS app is running in? (development, test, production?). There's nothing in process.env indicating an environment...
I'm aware that you can declare variables in your configuration file under each environment, but that doesn't help if you are dynamically loading modules...
You can either check the environment by checking the app.settings.env (this will work in Express), or you can do it in a more direct way by checking process.env.NODE_ENV (the environment is the one found in that variable or 'development' by default < this also works in other libraries such as Socket.IO etc).
app.get('env') would also return the environment.
if ( app.get('env') === 'development' ) {
app.use(express.errorHandler());
}
I'd like to address a straightforward way to passing NODE_ENV variables to your node script in order to access them in process.env
"scripts": {
"start": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon server.js",
"debug": "./node_modules/.bin/cross-env NODE_ENV=development ./node_modules/.bin/nodemon --debug server.js",
"test": "./node_modules/.bin/cross-env NODE_ENV=test ./node_modules/.bin/babel-tape-runner test/test-*.js"
},
can be used as
if ( app.get('env') === 'development' ) {
app.use(express.errorHandler());
}
The can detect which environment you are in by inspecting app.settings.env.
cannot access the nodejs server. can detect node env from express using app.setting.env
var app = express();
app.setting.env render to the template engine.
check from the browser.
There are a lot of useful recommendations in other answers. I'm generally doing it like this:
const environment = process.env.NODE_ENV || 'development';
The good thing is that such approach is not specific to Express per se, but actually is an accepted practice in wider Node.js ecosystem.
Also, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod.
Please see more details and use cases on the detect-environment's page.

Categories

Resources