Saw this code recently and can't seem to find the answer on google, so here we are.
The actual code was from a webpack config:
const {
NODE_ENV = 'production',
} = process.env;
Thanks for any help!
Saw this code recently and can't seem to find the answer on google, so here we are.
The actual code was from a webpack config:
const {
NODE_ENV = 'production',
} = process.env;
Thanks for any help!
Destructuring with a default value.
const { NODE_ENV } = process.env;
// is roughly:
const NODE_ENV = process.env.NODE_ENV
And together
const { NODE_ENV = 'production' } = process.env;
// is roughly (probably presence check is more sophisticated):
const NODE_ENV = process.env.NODE_ENV || 'production'
More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
This mean you assign default value to variable. look example below.
const { foo = "bar" } = "baz";
// foo -> "bar"
const { foo = "bar" } = { foo: "baz" };
// foo -> "baz"
This syntax assigns NODE_ENV
the value process.env.NODE_ENV
, or if that is falsy, 'production'
.. This is called destructuring and a few examples can be seen below:
Get key from object using default name
const my_object = {message: "hello"};
const {
message
} = my_object;
Get key from object using custom name
const my_object = {message: "hello"};
const {
message: my_message
} = my_object;