最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - variables I defined in webpack.DefinePlugin is undefined - Stack Overflow

programmeradmin0浏览0评论

webpack.config.js

const path = require('path')
HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index_bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
            plugins: ["@babel/plugin-proposal-class-properties"]
          }
        }
      },
      {
        test:/\.css$/,
        use:['style-loader','css-loader']
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      APIHOST: JSON.stringify('test'),
      BLOCKCHAINHOST: JSON.stringify('test')
    }),
    new HtmlWebpackPlugin({
      template: './src/template.html'
    }),
  ]
}

I defined 2 variables APIHOST and BLOCKCHAINHOST and I tried to console log this in reactjs App.js like so

ponentDidMount() {
    console.log(APIHOST)
}

The error I'm getting is APIHOST is undefined. I'm not sure what to do here, I've tried adding single quotes for webpack.defineplugin so it looks like 'APIHOST': JSON.stringify('test') but it's still giving me the same error.

webpack.config.js

const path = require('path')
HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'index_bundle.js',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ["@babel/preset-env", "@babel/preset-react"],
            plugins: ["@babel/plugin-proposal-class-properties"]
          }
        }
      },
      {
        test:/\.css$/,
        use:['style-loader','css-loader']
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      APIHOST: JSON.stringify('test'),
      BLOCKCHAINHOST: JSON.stringify('test')
    }),
    new HtmlWebpackPlugin({
      template: './src/template.html'
    }),
  ]
}

I defined 2 variables APIHOST and BLOCKCHAINHOST and I tried to console log this in reactjs App.js like so

ponentDidMount() {
    console.log(APIHOST)
}

The error I'm getting is APIHOST is undefined. I'm not sure what to do here, I've tried adding single quotes for webpack.defineplugin so it looks like 'APIHOST': JSON.stringify('test') but it's still giving me the same error.

Share Improve this question edited Oct 18, 2020 at 13:34 Tony 20.2k7 gold badges41 silver badges62 bronze badges asked Sep 22, 2019 at 22:20 alexWalexW 3034 silver badges14 bronze badges 4
  • 1 Try requiring webpack: const webpack = require('webpack') – Chris B. Commented Sep 22, 2019 at 22:31
  • If the app piled and gave undefined I think that webpack is being required. What I would like to see is the webpack output from the console. Or maybe a Codesandbox where I can play with the minimum possible code. – Rubén S. García Commented Sep 22, 2019 at 22:47
  • @RutherfordWonkington you're right how did i miss that – alexW Commented Sep 23, 2019 at 3:31
  • 1 I finally realize this is a piling error or warning. If you don't have the eslint-loader in your webpack config, you should try this:new webpack.optimize.UglifyJsPlugin({ minimize : true, press : { warnings : false } }), if you have eslint in your project, try to add globals: {"APIHOST":true} in your .eslintrc.json – JackLi Commented Dec 31, 2019 at 3:57
Add a ment  | 

2 Answers 2

Reset to default 4

You can do like this

plugins: [
    new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': JSON.stringify('development')
        }
    })
],

Then in your code

process.env.NODE_ENV

The version I'm using is

"webpack": "^4.29.6"

It looks like this is a known issue:

https://github./webpack/webpack/issues/1977

DefinePlugin doesn't work inside React Components

Fixed later on in Webpack 3:

This is fixed. Since webpack 3, the parser now fully understands ES6 semantics.

What version are you using? Does it make sense to upgrade?

发布评论

评论列表(0)

  1. 暂无评论