DEV Community

Discussion on: Runtime Configurations with React

Collapse
 
matt_catalfamo profile image
Matt Catalfamo

Hey,

If you are using webpack, in your local webpack configuration you can create the file using the create-file-webpack plugin (npmjs.com/package/create-file-webpack)

Then your plugin configuration would look something like this:

plugins: [
  new CreateFilePlugin({
    path: `your public folder`,
    fileName: 'runtime-config.js',
    content: `window.runConfig=${JSON.stringify(
      {
        apiUrl: 'test.com/api'
      }
    )};`,
  }),
]

And if you wanted to load your config from a file you can import it like:

const myConfigVar = require('./your-config.json');

and replace { apiUrl: 'test.com/api'} with myConfigVar