dotenv-webpack icon indicating copy to clipboard operation
dotenv-webpack copied to clipboard

dotenv-webpack not working. env file not loaded. variables are undefined

Open wibed opened this issue 1 year ago • 6 comments

using esm module based project

{
  "start": "npm run clean && cross-env NODE_ENV=development NODE_OPTIONS=\"--loader=ts-node/esm --trace-warnings\" webpack serve --mode=development --config webpack.config.ts"
}

dotenv-webpack does not prevent the app from starting, any env variable ends up beeing undefined. other plugins work as expected, thats why i dont assume my webpack.config.ts to be wrong

i load it as usual as new Dotenv() entry in the plugin section:

...
import Dotenv from 'dotenv-webpack'
...
  plugins: [
    miniCSSPlugin,
    copyPlugin,
    htmlPlugin,
    forkCheckerPlugin,
    new Dotenv()
  ],
...

wibed avatar Apr 24 '23 00:04 wibed

Running into this as well; it seems like it's not loading the .env file no matter what I try

TranquilMarmot avatar Oct 03 '23 21:10 TranquilMarmot

Have you tried this?

package.json: "build:local": "webpack --mode development --env NODE_ENV=local",

webpack config:

module.exports = async (env, options) => {
...

      new DotenvWebpackPlugin({
        systemvars: true,
        path: `./env/.env.${env.NODE_ENV}` // This means the env files are in a folder called `env` at the root
      }),

cosi-conda avatar Oct 13 '23 22:10 cosi-conda

for me this is not working right.

i had some success installing process, but it remained empty even after setting up ProvidePlugin, further specifying process: process/browser

accessible but remained empty. the path was set up as explained above.

wibed avatar Oct 14 '23 07:10 wibed

The problem might be legitimate. I don’t have a lot of experience with module npm support. Anyone else here able to check out my code to see what’s going on?

also, if anyone has a simplified boilerplate here I could download that and give it a try.

mrsteele avatar Oct 18 '23 06:10 mrsteele

We are using a Lerna Monorepo to manage multiple applications within a single repository. At the root level, we have a .env file where we define variables like:

NODE_ENV=development
IS_MSW_ENABLED=true

However, when we start the application using the command below, it throws the error "process is undefined". We are accessing IS_MSW_ENABLED in the index.ts file as shown:

process.env.IS_MSW_ENABLED

The command used to start the application is:

$ npx lerna run msw:start --scope=app-name

In our webpack configuration, we use the following to load the environment variables:

new Dotenv({
  path: '../../../.env',
}),

Should I run export IS_MSW_ENABLED=true in the terminal to resolve this issue?

 "npm": "^10.2.4",
 "node": "^20.9.0"
 "lerna": "^7.4.1"

@mrsteele Let me know for more info

nthombare-mdsol avatar Jul 23 '24 16:07 nthombare-mdsol

@nthombare-mdsol could you make a simplified repo so I can see the rest of your setup? I've been waiting for more info here and having a hard time solving without more context.

mrsteele avatar Jul 24 '24 02:07 mrsteele