Webpack environment variable NODE_ENV vs cli params --env - javascript

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.

Related

Detecting Webpack mode in JavaScript files

I am building a bundle of ReactJS files with Webpack.
Is it possible to detect inside of JSX files which Webpack mode is being used: "none", "development", or "production"?
For example, I would like some console.log() statements and global variables in development mode but not production.
One can build the Webpack bundle for development with:
$ npx webpack --config webpack.config.js --mode=development
and for production with:
$ npx webpack --config webpack.config.js --mode=production
To know whether the current build is in development mode or production mode, webpack.DefinePlugin() should be used in webpack.config.js:
const webpack = require('webpack');
module.exports = (env, argv) => {
return {
// other directives...
plugins: [
new webpack.DefinePlugin({
PRODUCTION: JSON.stringify(argv.mode === 'production'),
}),
],
}
};
Then in the bundled JavaScript files one can have this checking:
if (PRODUCTION) {
console.log("This is a production build.");
} else {
console.log("This is a development build.");
}
Note that PRODUCTION here will work not as a global variable, but as a template variable resolved to true or false.

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

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

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

Conditional requires with browserify

with browserify, I'm trying to require a module only when in development. For some reason it is always being included in production too.
NPM Scripts:
"start": "export NODE_ENV=development&& grunt watch & grunt serve",
"prod": "export NODE_ENV=production&& grunt prod"
Component requires:
const isProduction = process.env.NODE_ENV === 'production';
require('animation.gsap');
if (!isProduction) {
require('debug.addIndicators');
}
browserify bundles at compile time. So it is unaware of your variables. Check this package https://www.npmjs.com/package/conditionalify. It can solve your problem (though I have not use it) . You can have different context for different environment while bundling.

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