简体   繁体   English

电子main.js NODE_ENV不可用

[英]Electron main.js NODE_ENV is not available

So I am creating an application with: 所以我正在创建一个应用程序:

  • React 16.4.0 反应16.4.0

  • Electron 2.0.2 电子2.0.2

  • Webpack 4.11.0 Webpack 4.11.0

I am now able to compile and run the app with webpack ( webpack dev server ). 现在,我可以使用webpack( webpack dev server )编译并运行该应用程序了。 The problem is that I only want to show the Chrome dev tools in development mode. 问题是我只想在开发模式下显示Chrome开发工具。 This is something I can manage from the main.js file of Electron. 这是我可以从Electron的main.js文件管理的。 But the problem is that I do not want to do it manually. 但是问题是我不想手动进行。

So logically I want to do this via the process.env.NODE_ENV variable. 因此,从逻辑上讲,我想通过process.env.NODE_ENV变量执行此操作。 This variable is set by Webpack in the webpack-config. 该变量由Webpack在webpack-config中设置。 The problem is that when I try to access the variable in the main.js file I get an undefined instead of 'development' or 'production'. 问题是,当我尝试访问main.js文件中的变量时,得到的是undefined而不是“开发”或“生产”。


Webpack.common.js Webpack.common.js

const path = require('path');

module.exports = {
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'app.bundle.js'
  },
  resolve: {
    modules: [path.resolve(__dirname), 'node_modules']
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          cacheDirectory: true,
          presets: ['env', 'react'],
          plugins: ['transform-runtime'],
          env: {
            production: {
              presets: ['react-optimize']
            }
          }
        }
      }
    ]
  }
};

Webpack.dev.js Webpack.dev.js

const webpack = require("webpack");
const merge = require("webpack-merge");
const common = require("./webpack.common");
const path = require("path");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;

module.exports = merge(common, {
  mode: "development",
  entry: ["react-hot-loader/patch", path.join(__dirname, "src", "index.js")],
  devtool: "eval-source-map",
  plugins: [
    new BundleAnalyzerPlugin({ //Make bundle sizes visible
      analyzerMode: "static",
      openAnalyzer: false
    }),
    new webpack.HotModuleReplacementPlugin() // Enable hot module replacement
  ]
});

Since Webpack V4 the NODE_ENV must be set via the mode parameter. Webpack V4 NODE_ENV必须通过mode参数设置NODE_ENV


Main.js Main.js

Below is an abstract version of the file: 以下是该文件的抽象版本:

const isDevelopement = (process.env.NODE_ENV === "development");

console.log("Result: ", process.env.NODE_ENV); // Results in Undefined

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: false, // For security reasons
      devTools: isDevelopement
    }
  });
}

So I was wrong. 所以我错了。 Webpack mode sets the iternal NODE_ENV only for compile time. Webpack mode仅在编译时设置iternal NODE_ENV。 It does NOT update the global NODE_ENV . 更新全局NODE_ENV So you must still use the webpack.definePlugin : 因此,您仍然必须使用webpack.definePlugin

new webpack.DefinePlugin({ //Set the node env so that the project knows what to enable or disable
    'process.env': {
      'NODE_ENV': JSON.stringify('development')
    }
})

Now I can access the NODE_ENV variable in my application. 现在,我可以在应用程序中访问NODE_ENV变量。 But I can still not access this variable in the main.js file of Electron. 但是我仍然无法在Electron的main.js文件中访问此变量。

Why does this result in undefined and what should I change? 为什么会导致未定义,我应该更改什么?

Try to read mode through process.env.WEBPACK_MODE . 尝试通过process.env.WEBPACK_MODE读取mode

In your case: 在您的情况下:

const isDevelopement = (process.env.WEBPACK_MODE === "development");

Another, workaround solution using WebpackDefinePlugin : 使用WebpackDefinePlugin的另一种解决方法:

const mode = process.env.NODE_END || 'development';

module.exports = merge(common, {
  mode: mode,
  plugins: [
    new webpack.DefinePlugin({
      'WEBPACK_MODE': JSON.stringify(mode),
    })
  ]
});

and then you should be able to access it through process.env.WEBPACK_MODE . 然后您应该可以通过process.env.WEBPACK_MODE访问它。

Sorry, but process.env.NODE_ENV is an environment variable. 抱歉, process.env.NODE_ENV是环境变量。 This should not be set by some application in my opinion. 我认为这不应由某些应用程序设置。

However, if you want to set it up for your application why don't you just add it to your package.json file ("scripts" section), like: 但是,如果要为应用程序设置它,为什么不将其添加到package.json文件(“脚本”部分),例如:

"start_vdev": "NODE_ENV=development electron index.js",
"start_vprod": "NODE_ENV=production electron index.js"

Information on how to set up NODE_ENV in your OS can be found here: process.env.NODE_ENV is undefined 有关如何在操作系统中设置NODE_ENV的信息,请参见: process.env.NODE_ENV未定义

I'm not using Webpack for my particular build of Electron, but I did encounter the same problems. 我没有将Webpack用于我的特定版本的Electron,但是确实遇到了相同的问题。 I ended up with a solution that used Electron's ipcMain and ipcRenderer modules to get around it . 我结束了与使用电子的ipcMainipcRenderer模块的解决方案绕过它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM