mrsteele / dotenv-webpack

A secure webpack plugin that supports dotenv and other environment variables and only exposes what you choose and use.
MIT License
1.3k stars 74 forks source link

i'm not able to access .env variables please help me thanks in advance. #481

Closed RAzaALy closed 2 years ago

RAzaALy commented 2 years ago

webpack-common.js

const path = require('path');

const CopyWebpackPlugin = require('copy-webpack-plugin'); const Dotenv = require('dotenv-webpack');

const CURRENT_WORKING_DIR = process.cwd();

module.exports = { entry: [ '@babel/polyfill', path.join(CURRENT_WORKING_DIR, 'client/app/index.js') ], resolve: { extensions: ['.js', '.json', '.css', '.scss', '.html'], alias: { app: 'client/MONGO_URIapp' } }, module: { rules: [ { test: /.(js|jsx)$/, loader: 'babel-loader', exclude: /(node_modules)/ } ] }, plugins: [ new Dotenv({ path: path.resolve(__dirname, './.env.expample') }), new CopyWebpackPlugin([ { from: 'client/public' } ]) ] };

RAzaALy commented 2 years ago

webpack 4

mrsteele commented 2 years ago

How are you accessing the variables, and can you give us a glimpse of what your env file looks like?

(also, it’s a bit concerning you are using an example file for your actual envs, I’m imagining that may just be a placeholder during development).

mrsteele commented 2 years ago

Closing due to lack of participation and cannot reproduce.

Feel free to reopen this if you can provide more details, thanks.

afsheen-a commented 2 years ago

Facing the same issue. My configurations are: webpack.config.js:

plugins: [
      new DotEnv({ path: `./.env.${env.env}`, systemvars: true }),
      new CopyWebpackPlugin({
        patterns: [
          {
            from: path.resolve(__dirname, 'public'),
            to: path.resolve(__dirname, 'dist'),
          },
        ],
      }),
    ],

.env.local:

API_BASE_URL=https://my-url

index.js:

const BASE_URL = process.env.API_BASE_URL;
console.log(process.env) //this gives me an empty object
console.log(process.env.API_BASE_URL) //undefined
afsheen-a commented 2 years ago

Not sure what I need to change, thanks for any help/input you have!