-
Notifications
You must be signed in to change notification settings - Fork 213
Setting Environment Variables #82
Comments
You could do this with DefinePlugin: const globals = {
__REACT_APP_FOO__: process.env.REACT_APP_FOO
__REACT_APP_BAR__: process.env.REACT_APP_BAR
};
module.exports = ({ config }) => {
config.plugin('globals')
.use(webpack.DefinePlugin, globals);
}); If you use ESLint you'll also have to add them to the globals there. The snippet below is not entirely correct as they will be defined as writable but I don't know the correct way of doing it due to differences between the eslintrc and ES CLI syntax. One expects an array while the other wants an object, but any attempt at setting writable to config.module.rule('lint')
.loader('eslint', props => merge(props, {
options: {
globals: Object.keys(globals)
}
})); |
Rereading your question, I think webpack.EnvironmentPlugin might be better suited to that. In your case, assuming the environment variables get set during the CI process, you can pass them to your code like this: config
.plugin('env')
.use(webpack.EnvironmentPlugin, ['REACT_APP_FOO', 'REACT_APP_BAR']); After which you have them available in code as However, EnvironmentPlugin is already being used to inject NODE_ENV, so it will have to be merged with the existing arguments, which you can do like this (pulled from webpack-chain docs): config
.plugin('env')
.inject((Plugin, args) => new Plugin([...args, 'REACT_APP_FOO', 'REACT_APP_BAR'])); |
thanks for the help @pleunv! the goal is to have the environment variables read from hidden, uncommitted files files during the execution of the |
I wrote the original implementation of environment variables in create-react-app, and am sad to say that I just haven't put this in a preset yet. This doesn't really make sense for a Node.js preset, since it can read from the environment at execution time. For the web preset, maybe it'd be nice if there was a way to specify additional environment variables in package.json. Extending the environment variables used in |
Ha! That's awesome, I'm a fan of your work on create-react-app :). Ok cool, the only problem with defining variables in |
My Ops team needs to assign those at runtime. How can we do that? I'm using New issue for it: #319 |
You can add module.exports = {
use: [
...
['@neutrinojs/env', ['MY_VAR']], // 1
//['@neutrinojs/env'], // 2 - does not work
]
}; For case 1, however, if |
Hi! The ability to set default values has been added to the web preset in #983, which will be in the upcoming Neutrino 9. In the meantime (or for presets that don't inherit from web, so don't have the built-in const { EnvironmentPlugin } = require('webpack');
module.exports = {
use: [
['<existing preset name>', {
// Options
}],
(neutrino) => {
neutrino.config
.plugin('env')
.use(EnvironmentPlugin, [{
MY_VAR: 'default-value',
}]);
}
]
}; |
Oh lovely, this is precious! |
I am using const { EnvironmentPlugin } = require("webpack");
module.exports = {
use: [
[
"@neutrinojs/react-components",
{
devServer: {
// Disabling options.hot will also disable devServer.hot
hot: true,
// Proxy requests that don't match a known file to the specified backend.
proxy: "https://127.0.0.1:9000/v1"
}
}
],
neutrino => {
neutrino.config
.plugin("env")
.use(EnvironmentPlugin, [{ TEST: JSON.stringify("test") }]);
// .use(EnvironmentPlugin, ["URL", "TOKEN", "PROJECT"]);
}
]
}; However it does not inject variables to UpdateUsing .use(DefinePlugin, [
{ "process.env": { test: JSON.stringify("test") } }
]); Works just fine |
Hi! Testing your snippet works for me locally using Neutrino 8.3.0? Though note the EnvironmentPlugin docs say:
...so the Failing that, I would:
|
In order to have custom variables available per environment for API keys and such, without committing anything to version control, in https://github.com/facebookincubator/create-react-app for example I can do the following:
package.json
:add the following files to the root:
.env.dev
,.env.stg
,.env.prd
define unique variables per environment to the
.env
files such as:How would I accomplish this using Neutrino? I've tried replicating the same process using the
neutrino start
andneutrino build
scripts but the environment variables end up undefined.The text was updated successfully, but these errors were encountered: