0

if I have a json file as below, I am using React (or even JavaScript is also fine), can somebody help me reading this file depending upon the Environment I set, like if I set it to local, all I need is to load the settings of the local and if I set it as Prod, all the settings of the prod must be loaded using React script or JavaScript, any ideas Geeks? - thanks in advance.

[
    {
        "Environment": "local",
        "local": {
            "url-1": "https://www.google.com",
            "url-2": "https://www.msn.com"
        },
        "Development": {
           "url-1": "https://www.google.co.in",
           "url-2": "https://www.msn.co.in"
        },
        "Test": {
           "url-1": "https://www.somedifferenturl.co.in",
           "url-2": "https://www.somedifferenturl.co.in"
        },
        "Prod": {
           "url-1": "https://www.somedifferenturl2.co.in",
           "url-2": "https://www.somedifferenturl2.co.in"
        }
    }
]
2
  • Is this a react-native or electron application or something? Commented Oct 17, 2019 at 14:08
  • If bootstrapped using create-react-app, it should be easy for you.. create-react-app.dev/docs/adding-custom-environment-variables. You can create files like .env, .env.local, .env.production with the environment variables... And start application setting NODE_ENV=test|production|development. Make sure don't set any passwords/secrets in it as its available in browser Commented Oct 17, 2019 at 14:25

3 Answers 3

1

You can import the json file and set it on state.

import envConfig from './envConfig.json'

state = { envConfig }

And the have a helper function to read the Environment value and then update the state to have those values.

This would be the envConfig.json:

[{
    "Environment": "local",
    "local": {
        "url-1": "https://www.google.com",
        "url-2": "https://www.msn.com"
    },
    "Development": {
       "url-1": "https://www.google.co.in",
       "url-2": "https://www.msn.co.in"
    },
    "Test": {
       "url-1": "https://www.somedifferenturl.co.in",
       "url-2": "https://www.somedifferenturl.co.in"
    },
    "Prod": {
       "url-1": "https://www.somedifferenturl2.co.in",
       "url-2": "https://www.somedifferenturl2.co.in"
    }
}]
Sign up to request clarification or add additional context in comments.

3 Comments

evalmachine.<anonymous>:1 import envConfig from './envConfig.json' ^^^^^^^^^ SyntaxError: Unexpected identifier at new Script (vm.js:83:7) at createScript (vm.js:277:10)
He doesn't have a ./envConfig.json file to import from.
[link] (pastebin.com/kNJWpjhJ) That is the error he will get.
0

If you are using webpack you can make use of the DefinePlugin.

https://webpack.js.org/plugins/define-plugin/#usage

In your webpack config, in the plugins section you can add the DefinePlugin.

plugins: [
    // Some other plugins already defined.
    new webpack.DefinePlugin({"process.env.MY_ENV": JSON.stringify(process.env.MY_ENV)})
         ]

This rewrites all the occurences of process.env.MY_ENV in your JS files with the actual environment variable MY_ENV when you run the code through webpack.

And given a config.json like below

{
    "local": {
        "url-1": "https://www.google.com",
        "url-2": "https://www.msn.com"
    },
    "Development": {
        "url-1": "https://www.google.co.in",
        "url-2": "https://www.msn.co.in"
    },
    "Test": {
        "url-1": "https://www.somedifferenturl.co.in",
        "url-2": "https://www.somedifferenturl.co.in"
    },
    "Prod": {
        "url-1": "https://www.somedifferenturl2.co.in",
        "url-2": "https://www.somedifferenturl2.co.in"
    }
}

It can be used in a React component like below.

import React from "react";
import config from "./config.json";

function App() {
  const environment = process.env.MY_ENV;
  const envConfig = config[environment];
  // Do anything with the envConfig
  console.log(envConfig);

  return <h1>Hello</h1>;
}

export default App;

Starting your server with command MY_ENV=VirtualEnv yarn start, webpack converts the code to

function App() {
  const environment = "VirtualEnv";
  const envConfig = _config_json__WEBPACK_IMPORTED_MODULE_1__[environment]; // Do anything with the envConfig

  console.log(envConfig);
  return react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("h1", {
    __source: {
      fileName: _jsxFileName,
      lineNumber: 10
    },
    __self: this
  }, "Hello");
}

Comments

0

I would use the case method, combined with const localCall = require("http://localhost/pathtoyourscript.js or URL here") for a local call, const dev = require("URL") etc.

Example:

switch(envSet) {
  case 'local':  // if (envSet === 'local')
    const localCall = require("http://localhost/pathtoyourscript.js or URL here");
    [break]

  case 'dev':  // if (envSet === 'dev')
    ...
    [break]
//add as many cases as needed
  default:
    ...
    [break]
}

etc.

1 Comment

This is JavaScript by the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.