English 中文(简体)
Unable to access process.env variables with dotenv-webpack
原标题:

I m trying to access my .env file variables using dotenv-webpack so that they can be bundled later and I can use them on my website, however I followed the instructions and have looked online and can t make it work.

//.env
VAR=1234
// created a webpack.config.js with the following code
const Dotenv = require( dotenv-webpack );

module.exports = {
  plugins: [
    new Dotenv()
  ]
};
// index.js
console.log(process.env.VAR);
// undefined

I m not sure what to do because I ve followed the example in: https://github.com/mrsteele/dotenv-webpack and it seems like it should be easy...

问题回答

As suggested by Pandhu Wibowo, please give him the thumbs up, I managed to make it work using webpack-cli, webpack and dotenv packages. How to pass .env file variables to webpack config?

./env 
VAR=1234
./webpack.config.js
const webpack = require( webpack ); 

// replace accordingly  ./.env  with the path of your .env file 
require( dotenv ).config({ path:  ./.env  }); 

const path = require( path );

module.exports = {
  entry:  ./src/index.js ,
  output: {
    path: path.resolve(__dirname,  dist ),
    filename:  bundle.js ,
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": JSON.stringify(process.env)
    }),
  ]
};
./package.json
  "scripts": {
    "start": "webpack"
  },

npm run start
--> creates the bundle.js which will include the env variable

If you would like to use dotenv-webpack package to define environments files, you need to tell it where is your .env file, here s what I did:

webpack.dev.js

  • Import it:
const Dotenv = require( dotenv-webpack );
  • Use it in plugins array and pass to it .env.developement path:
plugins: [
      ...,
      new Dotenv({
        path: `${environmentsPath}/.env.development`,
        systemvars: true, //Set to true if you would rather load all system variables as well (useful for CI purposes)
      }),
    ],

webpack.prod.js

  • Import it:
const Dotenv = require( dotenv-webpack );
  • Use it in plugins array and pass to it .env path:
plugins: [
      ...,
      new Dotenv({
        path: `${environmentsPath}/.env`,
        systemvars: true, //Set to true if you would rather load all system variables as well (useful for CI purposes)
      }),
    ],

The good thing I like about this approach is that you can override the environment file using env-cmd package (e.g: "build:staging": "env-cmd -f environments/.env.staging webpack --config buildTools/webpack.prod.js --progress --color")

Please let me know if it helps.

Why you don t use dotenv package?

dotenv

This was the complementary point for me:

To access environment variables from the client app they must be prefixed with REACT_APP_. Otherwise they will only be accessible on the server side.

so my .env file looks like

REACT_APP_API_KEY=my-api-key
REACT_APP_SECRET=secretInfo2

With the help of: https://jasonwatmore.com/post/2022/06/22/react-access-environment-variables-from-dotenv-env

I was just trying to access my .env variables during development, not even building my app yet, and couldn t access any through the other methods using dot-env-webpack or DefinePlugin. This way worked out for me:

new webpack.EnvironmentPlugin({
  NODE_ENV:  development , // use  development  unless process.env.NODE_ENV is defined
  DEBUG: false,
});

Read more: https://webpack.js.org/plugins/environment-plugin/





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签