How to pass .env file variables to webpack config?

后端 未结 7 856
旧时难觅i
旧时难觅i 2020-12-05 05:18

I am new to webpack and worked out almost all build sections, but now the problem is that I want to pass the environment variables from a .env file to webpack config, so tha

相关标签:
7条回答
  • 2020-12-05 05:52

    webpack + dotenv

    I did get inspiration from the accepted answer, but it doesn't work for me. Maybe the API of dotenv has changed.

    The following works for me

    import dotenv from 'dotenv'
    import { DefinePlugin } from 'webpack'
    
    
    ...
    
    plugins: [
        new DefinePlugin({
          'process.env': JSON.stringify(dotenv.config().parsed)
        })
    ]
    
    ...
    
    0 讨论(0)
提交回复
热议问题