Which file does process.env.NODE_ENV read in the browser or in the node environment?
The
process object is a global variable of node and provides information about the current Node process. The
process.env property returns an object containing all the environment variables of the current shell
.
in webpack , we often create an environment variable NODE_ENV to determine whether we are in a development environment | production environment | Test environment
my question is, where do you usually set the variable NODE_ENV ?
1. package.json file can be set to
module.exports = {
build: {
env: {
NODE_ENV: ""production""
}
},
dev: {
env: {
NODE_ENV: ""development""
}
}
}
so in the build folder, such as webpack.dev.conf.js file ( node environment?) Read the NODE_ENV value defined by the file that process.env.NODE_ENV is read?
and in a file under src (browser environment? ) which file is read process.env.NODE_ENV ?
first of all, the landlord understands that process.env is a variable defined by the Node environment, and the browser environment must return undefined .
1, first of all, how to let Node to identify which variable is currently running, whether it is "development" or "production" , which is defined by cross-env . It is a cross-platform tool ( windows or maxos or liunx ) that identifies the currently running environment variables. Of course, to check its documentation, it is more intuitive.
2, how do I get the browser to identify the currently running environment variables? In fact, webpack has a plug-in, which is webpack.DefinePlugin
.
new webpack.DefinePlugin({
// process.env
'process.env': require('./config/dev.env')
}),
that's why browsers recognize process.env.NODE_ENV
< hr >
if it is helpful, please click to adopt it, thank you ~
read webpack4 idebar/Sidebar.jsx" rel=" nofollow noreferrer "> official website document Mode , which mentions
.
If not set, webpack sets production as the default value for mode. The supported values for mode are:
if the mode value is not set, the default mode value is "production" . Setting process.env.NODE_ENV can also be read through the mode value. After testing, it seems that the value of mode is finally read
.
is like this. Vue runs on web, and there is no variable of node or api (including process )
.
make the api of node valid on the web side through the configuration of module.exports.node of webpack.base.config.js , so there is no environment variable corresponding to process.env on the web side, just an empty object, so it is convenient to mount NODE_ENV
of the production environment or the development environment.
process.env.NODE_ENV reads global, not file, just like the window object on the browser side, on which you mount a variable. Other files can be accessed for the same reason