I have read all of the articles here at stack overflow and about 200 more from google searches and i, for the life of me, can't get my head around process.env. No matter what i do it's always an empty object.
我已经阅读了stack overflow的所有文章,从谷歌搜索到大约200篇文章,我,为了我的生活,无法理解过程。无论我做什么,它总是一个空的物体。
const path = require("path");
const webpack = require('webpack');
module.exports = {
entry: {
search: './src/search/search_index.js',
// search: './src/search/search_index.js'
},
output: {
path: path.join(__dirname, "../website/public/javascript/react"),
publicPath: '/',
filename: "[name].bundle.js"
},
module: {
loaders: [{
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
devServer: {
historyApiFallback: true,
contentBase: './'
},
plugins: [
new webpack.DefinePlugin({
"process.env" : {
'NODE_ENV': '"production"'
}
})
]
};
"scripts": {
"start": "webpack-dev-server --inline --hot --content-base src/search",
"test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive ./test",
"test:watch": "npm run test -- --watch"
}
console.log(process.env);
// from a render method in the running application started with "npm start"
console.log(process.env);//在运行中的应用程序中的呈现方法从“npm启动”开始
Always returns {}
总是返回{ }
I am trying to get NODE_ENV set to production on my build and have had zero luck %NODE_ENV% show production but regardless i get the warning message in console about it running on a slower build to use NODE_ENV production.
我试图在我的构建上将NODE_ENV设置为production,并且已经没有什么好运气的%NODE_ENV%显示生产,但是不管怎样,我在控制台中得到了使用NODE_ENV生产在较慢的构建上运行的警告消息。
To try and unwind what's happening i just wanted to see what my app was getting in process.env and it's an empty object. So then i tried hard coding it with defineplugin to again just test whats going on and it's the same result.
我只是想看看我的应用程序是如何运行的。env是一个空对象。然后我尝试用defineplugin对它进行硬编码来测试到底发生了什么,结果是一样的。
Been banging my head against this for about 3 hours now. Any help is appreciated.
我用头撞墙已经有三个小时了。任何帮助都是感激。
1 个解决方案
#1
3
The DefinePlugin
configuration you're using replaces occurrences of process.env.NODE_ENV
expressions in your code with whichever value you configure - it won't touch references to process.env
, as you haven't told it to do anything with that exact expression - the configuration you're using is just shorthand for replacing multiple expressions which share a common root.
您正在使用的DefinePlugin配置取代process.env的出现。代码中的NODE_ENV表达式,无论您配置哪个值——它都不会涉及到对进程的引用。env,因为您还没有告诉它对那个确切的表达式做任何事情——您正在使用的配置只是替换共享一个公共根的多个表达式的简写。
Webpack is detecting that you're referencing a global process
variable and is injecting the process
mock from node-libs-browser, which as you can see defines an empty env
object.
Webpack检测到您正在引用一个全局进程变量,并且正在从node-libs浏览器中注入进程模拟,您可以看到,该浏览器定义了一个空的env对象。
You can use node
Webpack config to control this. Adding node: {process: false}
to your config will disable injection of a mock for process
.
您可以使用node Webpack配置来控制它。在配置中添加node: {process: false}将禁用流程模拟的注入。
#1
3
The DefinePlugin
configuration you're using replaces occurrences of process.env.NODE_ENV
expressions in your code with whichever value you configure - it won't touch references to process.env
, as you haven't told it to do anything with that exact expression - the configuration you're using is just shorthand for replacing multiple expressions which share a common root.
您正在使用的DefinePlugin配置取代process.env的出现。代码中的NODE_ENV表达式,无论您配置哪个值——它都不会涉及到对进程的引用。env,因为您还没有告诉它对那个确切的表达式做任何事情——您正在使用的配置只是替换共享一个公共根的多个表达式的简写。
Webpack is detecting that you're referencing a global process
variable and is injecting the process
mock from node-libs-browser, which as you can see defines an empty env
object.
Webpack检测到您正在引用一个全局进程变量,并且正在从node-libs浏览器中注入进程模拟,您可以看到,该浏览器定义了一个空的env对象。
You can use node
Webpack config to control this. Adding node: {process: false}
to your config will disable injection of a mock for process
.
您可以使用node Webpack配置来控制它。在配置中添加node: {process: false}将禁用流程模拟的注入。