webpack react process.env 始终为空(Windows 10) [英] webpack react process.env always empty (windows 10)

查看:50
本文介绍了webpack react process.env 始终为空(Windows 10)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了堆栈溢出中的所有文章,并从谷歌搜索中阅读了大约 200 多篇文章,我一生都无法理解 process.env.无论我做什么,它总是一个空对象.

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.

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);//从正在运行的应用程序中的渲染方法开始,以 "npm start"

console.log(process.env); // from a render method in the running application started with "npm start"

总是返回 {}

我试图在我的构建中将 NODE_ENV 设置为生产,并且 %NODE_ENV% 显示生产的运气为零,但无论如何我在控制台中收到警告消息,关于它在较慢的构建上运行以使用 NODE_ENV 生产.

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.

为了尝试解开正在发生的事情,我只是想看看我的应用程序在 process.env 中得到了什么,它是一个空对象.然后我尝试使用defineplugin对其进行硬编码,以再次测试发生了什么,结果是相同的.

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.

我已经用头撞了大约 3 个小时了.任何帮助表示赞赏.

Been banging my head against this for about 3 hours now. Any help is appreciated.

推荐答案

您使用的 DefinePlugin 配置替换了代码中出现的 process.env.NODE_ENV 表达式使用您配置的任何值 - 它不会触及对 process.env 的引用,因为您没有告诉它使用该确切表达式执行任何操作 - 您正在使用的配置只是替换的简写多个表达式共享一个公共根.

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.

Webpack 检测到您正在引用全局 process 变量并正在注入 process mock 来自 node-libs-browser,如您所见,它定义了一个空的 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.

您可以使用 node Webpack 配置控制这个.将 node: {process: false} 添加到您的配置中将禁用 process 的模拟注入.

You can use node Webpack config to control this. Adding node: {process: false} to your config will disable injection of a mock for process.

这篇关于webpack react process.env 始终为空(Windows 10)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆