Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin works only once #13

Open
sylver opened this issue Apr 12, 2018 · 16 comments
Open

plugin works only once #13

sylver opened this issue Apr 12, 2018 · 16 comments

Comments

@sylver
Copy link

sylver commented Apr 12, 2018

Once installed the plugin works as expected (loading my .env vars in process.env being used in webpack and all), but after a node reload or any changes, it suddenly stops to populate process.env.

I can make it to "work" again either by changing in .babelrc the path option value (which I should not need, my .env is at the root of my repository) or by running a new yarn install --dev babel-plugin-inline-dotenv to clean it up, which makes me wondered if there's some sort of cache messing up here ?
Once it stops working for a specific .env path, it won't use it again no matter what until reinstalled.

node version : 9.11.1

package.json (relevant part)

"@babel/cli": "^7.0.0-beta.42",
"@babel/core": "^7.0.0-beta.42",
"@babel/node": "^7.0.0-beta.42",
"@babel/preset-env": "^7.0.0-beta.42",
"@babel/preset-react": "^7.0.0-beta.42",
"@babel/preset-stage-0": "^7.0.0-beta.42",
"@babel/register": "^7.0.0-beta.42",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-inline-dotenv": "^1.1.2",
"babel-plugin-transform-require-ignore": "^0.1.1",

.babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-stage-0"
  ],
  "plugins": [
    ["inline-dotenv"]
  ],
  "env": {
    "development": {
      "plugins": [
        "react-hot-loader/babel"
      ]
    },
    "node": {
      "plugins": [
        "dynamic-import-node",
        [
          "transform-require-ignore",
          {
            "extensions": [".sass", ".scss", ".less"]
          }
        ]
      ]
    }
  }
}
@brysgo
Copy link
Owner

brysgo commented Apr 12, 2018

Hey thanks for reporting this, I think you are experiencing caching issues, when you change the environment variables you have to clear out your cache.

Unless you are saying that the variables aren't getting set at all.

@sylver
Copy link
Author

sylver commented Apr 12, 2018 via email

@brysgo
Copy link
Owner

brysgo commented Apr 12, 2018

Hmm... Could it be hot loading? If the process that watches your changes didn't have the environment variables set maybe that would do it? I'm not sure how to test this hypothesis.

@sylver
Copy link
Author

sylver commented Apr 13, 2018

@brysgo I thought about it but it seems unlikely, the fact env variables get unset is really random, either while the dev server is running (and reloading) or when running several prod builds in a row.

Maybe I did something wrong but I'm clueless here.

If you want you can give it a try with this repo : https://github.com/sylv3r/react-starter

@FadiAboMsalam
Copy link

am facing a bit different problem i actually followed nextjs example on with dot-env which uses this plugin
what happens with me is that the environment variable are always loaded but not changed
please have alook at my bug here https://github.com/zeit/next.js/issues/4748
i have changed one variable inside the .env and it still loads the old value of the variable
and yes it seems like caching issues but how to solve them :D
any suggestions ?

@FadiAboMsalam
Copy link

btw removing node_modules and reinstall them again seems to solve the issue
but it definitely not the best approach to solve this !

@brysgo
Copy link
Owner

brysgo commented Jul 9, 2018

@FadiAboMsalam - Unfortunately the transformed code is cached with the old variable values. You can remove just the cache (which is likely stored inside your node_modules folder in some cache dotfiles), but I would love to find a better solution to this!

timneutkens pushed a commit to vercel/next.js that referenced this issue Aug 8, 2018
## Issue

I decided to rewrite the [with-dotenv](https://github.com/zeit/next.js/tree/canary/examples/with-dotenv) using [dotenv-webpack](https://github.com/mrsteele/dotenv-webpack) example because:

- changes doesn't get applied  (#4748, brysgo/babel-plugin-inline-dotenv#13)
- the production mode doesn't work at all
- this approach has already been used in the [examples/relay-modern](https://github.com/zeit/next.js/blob/9320d9f006164f2e997982ce76e80122049ccb3c/examples/with-relay-modern/next.config.js)
- it is [documented](https://webpack.js.org/plugins/environment-plugin/#dotenvplugin) by webpack

## Alternatives

* remove/deprecate example
* fix babel-plugin-inline-dotenv

## Related

Closes: #4748
@andreametelli
Copy link

same problem. When I run

nodemon -w src --exec \"babel-node src/bin/www --presets es2015,stage-0\"

the first time it set env correctly. When I restart the server I see the env in the process.env but seems that babel not applies them to the project. Any suggestions?

@brysgo
Copy link
Owner

brysgo commented Jan 11, 2019

@andreametelli - you might be experiencing caching issues, make sure you don't have any cache directory set, and if you do, delete it

@andreametelli
Copy link

@brysgo I already deleted my cache. I noticed that using nodemon with babel-node the plugin loads the configuration only if I save the main file of my project also if I didn’t modified it. If I save an other file nodemon restarts correctly but without load the config.

@brysgo
Copy link
Owner

brysgo commented Jan 12, 2019

@andreametelli - interesting, could nodemon + babel-node be running the babel plugin from a different path?

@andreametelli
Copy link

andreametelli commented Jan 12, 2019

@brysgo I don’t think so, because i usually work without es6 modules. It was my first experience with them. Usually using nodemon with node command i can save any file and nodemon restarts the application correctly also loading all the configuration from dotenv. Using babel-node the behavior is different. As i told before, when i save a file that is not the main file it will restart without loading the specific envs.

@brunohkbx
Copy link

Any update on this?

@brysgo
Copy link
Owner

brysgo commented Mar 13, 2019

@brunohkbx - I'm not really sure how to triage this, but if you have any thoughts I'm all ears.

@ankhzet
Copy link

ankhzet commented Apr 25, 2019

For react-native folks: you should clear Metro cache (restart it with Shift+R) after changing .env file.

@beaumontjonathan
Copy link

For those stumbling on this: babels caching has no visibility into where environment variables are used, and so which files are invalidated by a change to your .env. (For those novices like me who want to take a peak, look in node_modules/.cache/@babel/register/.babel.X.Y.Z.dev.json and do a quick search for your env vars)

Disabling the babel cache prevents cached env values, but will increase build times. For my small project this is negligible and babel is still ⚡

Try running BABEL_DISABLE_CACHE=1 yarn ..., this works great for me with nodemon/TS for live-reloading when editing my .env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants