1

I have this code in envrionment.ts

export const environment = {
    production: false,
    host_name: 'http://local.website.com',
    api_name: 'http://local.website.com/api',
    ajax_name: 'http://local.website.com/ajax',
};

Once the app is built, we need to change the environment settings without undergoing another build. Essentially because the web-app is coupled to another website, and the back-end people want to change these settings without running a build. Using the .env root file has been ruled-out as insecure leaving a .json file in the assets folder as the only option. There is another ts file on the app that can read the json with no problem when served but envrionment.ts can not seem to, supposedly because its static. Any help would be appreciated.

6
  • are you asking to DYNAMICALLY change a STATIC file ? ;/ Commented May 18, 2021 at 9:35
  • You can't use environment.ts file after the build. That file is compiling and bundling into the production file so you need to do the rebuild after changing any value in it. You can refer jvandemo.com/… Commented May 18, 2021 at 9:41
  • So even if the data is loaded from a json file, the build will compile this within the app and not a references external file? I was told if the assets folder is completely ignored by the build and this can be used. Commented May 18, 2021 at 10:31
  • @noririco yes, essentially, is this possible? Commented May 18, 2021 at 10:32
  • @t-owens-elas you can try stackoverflow.com/questions/43651991/… Commented May 18, 2021 at 11:32

1 Answer 1

1

At the company I work for now, they adjust the settings via ftp. For this reason, it needs to read the json file and rewrite it every time the client is refreshed.

Follow the instructions in the url I shared. it worked for us.

https://christianlydemann.com/implementing-dynamic-environments-in-angular-for-avoiding-one-build-per-environment/

Edit : maybe you can use specifically created regex records. You can try changing it in main.bundle.js using the fs library belonging to nodejs. like this

export const environment = {
    production: '<<<<PRODUCTION>>>>',
    host_name: '<<<<HOST_NAME>>>>',
    api_name: '<<<<API_NAME>>>>',
    ajax_name: '<<<<AJAX_NAME>>>>',
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I did try that, its a legacy build and complicated solutions seem to cause bigger problem. All I really need to do is import the JSON info read by the app using ngInit() (which is does) only I need to import this to envrionment.ts. I think that should do it.
Thank you for the edit. Looks like an interesting solution. I am not sure how to alter things like that in Node directly. I will look into it. Any guidance you can give me is also appreciated.

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.