# Kotlin JS React accessing/configuring environment variables

**URL:** <https://discuss.kotlinlang.org/t/kotlin-js-react-accessing-configuring-environment-variables/16906>\
**Category:** JavaScript\
**Created:** [March 31, 2020, 3:45pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-react-accessing-configuring-environment-variables/16906 "2020-03-31T15:45:37Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![simonjacobs](https://avatars.discourse-cdn.com/v4/letter/s/d07c76/32.png) [@simonjacobs](https://discuss.kotlinlang.org/u/simonjacobs)\
**Post date:** [April 2, 2021, 1:12pm UTC](https://discuss.kotlinlang.org/t/kotlin-js-react-accessing-configuring-environment-variables/16906/9 "2021-04-02T13:12:11Z")

</div>

After a bit more thinking, I see you can pass arbitrary values to the Webpack configuration (and so your Kotlin program in turn if desired) from the Gradle build using the Webpack environment variables feature ([Webpack v4 docs](https://v4.webpack.js.org/api/cli/#environment-options) – note the Kotlin plugin currently uses this version). To do this you also need to adjust the Webpack configuration file so that it exports a function. To illustrate:

```auto
//build.gradle.kts
kotlin {
   js { // Kotlin plugin v1.4 syntax
      browser {
         webpackTask {
            args.plusAssign( listOf( "--env.arbitraryValue=fortyTwo" ) )
            webpackConfigApplier {
               export = false // stops default export of config object in Webpack code
            } 
         }
      }
   }
}

```

```auto
//webpack.config.d/additional-config.js
module.exports = env => {
   
  const definePlugin = new webpack.DefinePlugin(
      {
         REPLACE_ME: env.arbitraryValue
      }
   )
   config.plugins.push(definePlugin)

   return config
}

```

```auto
// In the Kotlin program
val theAnswer = js("REPLACE_ME") // sets theAnswer = "fortyTwo"

```

That should then allow multiple setups in multiple environments.

---

_[View the full topic](https://discuss.kotlinlang.org/t/kotlin-js-react-accessing-configuring-environment-variables/16906)._
