未加载环境变量

时间:2020-07-20 14:26:24

标签: javascript npm webpack environment-variables env-cmd

我正在使用env-cmd将环境变量文件加载到我的javascript项目中。 我有一个简单的例子:

console.log(process.env.HELLO);
在我的index.html中显示

,其中显示“未定义”。我不知道怎么了。

使用以下命令行运行项目:

env-cmd -f .env.staging webpack-dev-server --open

我的目标是针对每个环境使用具有多个配置文件的多个命令。

如果需要,请在我的webpack.config.js文件下面找到:

const path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');


module.exports = {
  mode: 'development',
  devtool: 'inline-source-map',
  devServer: {
    contentBase: './dist',
  },
  entry: './src/index.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
    ],
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
    hash: true,
    template: './src/index.html',
    filename: 'index.html'
  })
  ]
};

0 个答案:

没有答案