At work we are trying to use proxy.config.mjs
(located in src folder) as per Angular Docs to talk to back-end API in Dev mode but we want to set target
value in this proxy conf using environment.ts file.
Have followed all default configurations to set-up proxy.config.mjs
, environment
files but getting below error when doing ng serve
:
An unhandled exception occurred: Cannot find module '/proxy-test/src/environments/environment' imported from proxy-test/src/proxy.config.mjs
Other files :
src/environments/environment.ts
:
export const environment = {
production: false,
apiUrl: 'http://localhost:3000'
};
src/proxy.config.mjs
:
import { environment } from './environments/environment';
export default [
{
context: [
'/api'
],
target: environment.apiUrl, <- Trying to use env value here
secure: false
}
];
angular.json
:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "proxy-test:build",
"proxyConfig": "./src/proxy.config.mjs" <- Using proxy conf here
},
"configurations": {
"production": {
"browserTarget": "proxy-test:build:production"
},
"development": {
"browserTarget": "proxy-test:build:development"
}
},
"defaultConfiguration": "development"
}