question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Static config file at project root

See original GitHub issue

After searching through the issues I can’t seem to find anyone asking this, but if I have missed it please link me to it.

I know CRA has the concept of env variables but I am looking for a little more than this (I want to include objects and arrays). I want to have a config directory in the root of my project, and inside an index.js something like:

import local from './local';
import global from './global';
const env = require(`./${process.env.NODE_ENV}`);

export default { ...global, ...env.default, ...local };

This will give me a config file for each environment but also a local.js where I can override anything I need, for example during development I want to change an API_URL to a local branch.

My question is using CRA can I get webpack to recognize this file outside of the src directory? I don’t want to have to eject just to add some static config files.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
joshhornbycommented, Mar 29, 2017

For anyone finding this issue, a working solution has been to have a config folder at the project root, inside I have production.js and development.js, each file looks something like:

module.exports = { key: 'value' }

I then have a index.js:

const merge = require('lodash/merge');
const global = require('./global');

var env; // let doesn't seem to work here
if (process.env.NODE_ENV === 'development') {
    env = require('./development');
} else {
    env = require('./production');
}

module.exports = merge(global, env);

And then in code I have just import this file and get the values eg config.key

2reactions
joshhornbycommented, May 22, 2017

@gaearon With the 1.0 updates the method listed above (https://github.com/facebookincubator/create-react-app/issues/1469#issuecomment-290081143) breaks due to the config file being outside src.

What do you suggest now? I don’t think pulling the config folder into src makes sense, so would be interested to hear how you’d solve this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to manage static files (e.g. images, JavaScript, CSS)
Store your static files in a folder called static in your app. For example my_app/static/my_app/example.jpg . Serving the files. In addition to these ......
Read more >
Is there a way to put every configuration file to a config ...
1. Use IDE settings to hide config files. I recommend that you keep them in their default location since it is a common...
Read more >
The creeping scourge of tooling config files in project root ...
I don't get this, what's the problem here? Having config files in root means you know they can apply to all sub directories....
Read more >
Static files in ASP.NET Core | Microsoft Learn
Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed ...
Read more >
Config Files - Babel.js
For project-wide configuration, Babel will automatically search for a babel.config.json file, or an equivalent one using the supported extensions, in this root ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found