I’m working on an Angular application and need to determine whether it’s running in cloud or not. How can I check the mode programmatically within my Angular code? Are there specific Angular utilities or environment variables I should use to achieve this?
I’ve tried checking the environment files (environment.ts and environment.prod.ts) to see if there’s a variable indicating the environment mode but these file only let me see that whether i am in production or local mode.
3
After a spending a lot of time on it, I have got this solution:
Create an environment file for cloud mode, e.g., src/environments/environment.cloud.ts:
export const environment = {
production: false,
isCloudMode: true
};
Modify angular.json
: Add the cloud configuration to the build section:
"cloud": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.cloud.ts"
}
]
}
Also, add the cloud configuration under the serve section, don’t forget to add this otherwise you’re gonna get this error: An unhandled exception occurred: Configuration ‘cloud’ is not set in the workspace.
"serve": {
"configurations": {
"cloud": {
"buildTarget": "your-app-name:build:cloud"
}
}
}
Use the environment in your code:
import { environment } from 'src/environments/environment';
console.log(environment.isCloudMode);
Run the app in cloud mode:
ng serve --configuration cloud or ng serve --c cloud