I am trying to access a Linux environment variable in angular 2 but I am not sure how to do it. For example if I export a variable in terminal i can access it in node but I don't know how to access this variable in angular 2.
$ export HOST_IP="192.168.1.1"
I can access this environment variable in node like: process.env.HOST_IP. Could anyone please tell me how to access it in angular 2.
Thanks
NodeJS applications are run in a Server. So a NodeJS app can access the attributes of the server (Ex : Environmental variables).
But an Angular application runs on the browser. Thus it cannot access to the details of the machine that it is run on (there are some exceptions of course, but I'll not discuss them here).
Said that, if your true requirement is to change certain variables depending on the build of the Angular app (ex : test build, debug build, production deployment build), You can use the environment files. There are plenty of guidelines on the internet on how to do that. Few of them are,
https://alligator.io/angular/environment-variables/
https://medium.com/beautiful-angular/angular-2-and-environment-variables-59c57ba643be
https://medium.com/#natchiketa/angular-cli-and-os-environment-variables-4cfa3b849659
Related
I am working on a NodeJS application which is a containerized application. I use Jenkins to build and deploy it. I have an environment .env file and along with this, also have .env files based on environments.
For example: .env.DEV, .env.SQA, .env.STG and .env.PROD.
Each file has different values for the variables based on environments.
When I am deploying my application, it always fetches variables from the .env file instead of specific environment file i.e. .env.DEV (if deploying on DEV server).
How do we use specific environment file while doing the deployment on Jenkins?
Note - I followed this great content on dotenv library but I didn't find anything helpful for my use-case. I even Googled a lot but didn't find much on this. Any help would be greatly appreciated.
You can use dotenv-flow which does exactly this, given the value of NODE_ENV environment value it will load the expected environment.
You also will need to make sure that the container receives the proper environment values from jenkins, this might help.
I develop an application with nodejs and react. I use dotenv for configuration in my different environment.
I use TFS 2017 for build and release my application.
What is the best practise for add my .env file of production environment?
Production configs can be difficult to maintain. You can use any module or even write your own custom module to load environment variable in to your application.
However, maintaining production .env files locally for each product (i.e. committing them or putting them inside your docker image is a bad idea. If you ever happen to change some of those configs, you will have to manually change in the code for each application, build docker image (if required) and redeploy. If there are just a couple of applications, it might be easy. But if the number of related applications grow in size (as usually happens in case of microservice based architecture) - all of them sharing credentials, ips for database connections etc. it becomes a hectic task to manually change code and redeploy all the applications.
Usually, developers tend to keep a central repository of all credentials and environment variables. For examples, AWS offers parameter store and MS Azure offers Azure Key vault. In such a case, all the parameters are fetched during start time, and all we have to do is restart the application.
So usually people set only one global varibale NODE_ENV (as prod or dev), dynamically fetch all environment variables based on NODE_ENV running something like env $(node read-env-variables.js) node app.js.
I have an Angular4 web app, deployed on Azure. Now I want to deploy this app to other environments on Azure: one for testing, one for acceptance and one for production. Every environment has different API endpoints and may have other variables, like Application Insights. All those environments run Angular in production mode.
The way Angular advises you to do this, is by the Enviroment files (environment.test.ts, enviroment.acc.ts, environment.prod.ts). I could configure all the different API endpoints in those files, and run my build with --prod for production for example.
But that is not the way I want to do this. I want to use the exact same application package deployed to test for my acceptance environment, without rebuilding the project. In Visual Studio Online, this is also really simple to configure.
The point is: how can I make my API endpoints differ per environment in that way?
The way I want to do this, is by the App Settings in Azure. But Angular can't get to those environment variables because it's running on the client side. Node.js is running on serverside and could get those App Settings - but if that's the way I need to do it, how do I make Node.js (used in Angular4 CLI) to send those server variables to the client side? And what about performance impact for this solution?
How did you fix this problem for your Angular4 apps on Azure? Is it just impossible to fix this problem with the Azure App Settings?
For everyone with the same question: I didn't fix this problem the way I described above.
At the end, I did it the way Angular wants you to do it: so rebuild for dev, rebuild for acc and rebuild for prod.
In Visual Studio Online, at build time, it builds and tests our code and it saves the uncompiled/unminified code. At release time, it builds en tests it again and releases it to the right environment with the right environment variables (--prod for example).
I don't think there is another way to fix this.
The solution is pretty old school but it works! Although you can use branching or tag for this purpose instead of cloning the code to the package.
The best solution as you said is Azure app settings will be saved as environment variable so you should implement an API with node.js and share the variables you want.
Of course there is an impact because of additional http call, but it's just one time at application start which is about max 5ms and depends on each program policy whether is impact or not.
Another option could be move the variables to the JSON file in the asset folder, and change it at deploy runtime with release pipeline. that's easier implementation but the disadvantage is you will have to use release variables instead of app settings and if you have config changes you will have to update the variable value first and redeploy it, although that works most of the times but sometimes you want to change just like a connection string and you will have to redeploy.
I am trying to deploy an application prototype to openshift. It works locally with mongodb at 127.0.0.1. I am trying to get it to respect process.env.OPENSHIFT_MONGODB_DB_URL when in the openshift environment but that variable is not accessible to my nodejs cartridge at runtime.
I can see that it is being set in my application's shell environment. When I do rhc ssh and then export I see OPENSHIFT_MONGODB_DB_URL=[full_url_with_password] and it all looks good.
But when, in my node.js application, I call process.env.OPENSHIFT_MONGODB_DB_URL it returns undefined.
To double check, I did a console.log(util.inspect(process.env)) from within my node.js app, and what I saw was different from what I see within my appication's secure shell. No OPENSHIFT_MONGODB_* variables were in the environment that is exposed to my node.js app.
How can I access variables across different cartridges? Or is this a configuration error?
It sounds like a configuration error. I have a similar application and
console.log(util.inspect(process.env))
gives me a clear picture of the mongodb environment variables.
The developers page indicates that:
Database environment variables pertain to a database, if one exists, and are used to connect an application to a database. Note that these connections are only available to an application internally; you cannot connect from an external source.
This suggests, to me, that the nodejs is external to the mongodb installation. I have an idea that it can be verified with the command:
rhc app show OPENSHIFT_APP_NAME
It might lead to the source of the problem. A correctly configured app would have nodejs and mongodb in this list.
I am building a Node.js application and need to store database credentials (and other runtime properties) in such a way that they can be read when deployed on Heroku. My source is available in a public GitHub repository.
I am currently using environment variables, configured using heroku config:add, but am looking to understand if there are any alternatives. I would potentially like to use Cloud9 IDE, but it does not currently support environment variables.
Another option is to store the parameters in a config. file, but I believe the file would need to be checked in to Git (and as such, be publicly available) in order to be pushed to Heroku.
Thanks for your help.
ENV vars are generally considered the way to go, and the way Heroku do it themselves for database_urls and the like.
As you and your app are the only people with access to the env vars, you're generally OK security wise.
Putting credentials in Git or similar is a bad idea as it's another place that needs to be secured.
The one way I know of to solve the problem for development using command-line arguments. These can be specified in your run/debug configuration. You can then access the parameters in process.argv. Of course this means that they will be stored in your Cloud9IDE dev environment. You could then use the ENV variables in a retail production. This will at least prevent the credentials from being visible in source or config files.