When attempting to access the environment variable in an Angular 9 library, I encountered an error during the library build process.
Here is how it was implemented:
import { EnvironmentViewModel } from 'projects/falcon-core/src/lib/view-models/environment-view-model';
class EnvironmentImpl implements EnvironmentViewModel {
production = false;
openID = {
authority: "https://dev-816623.okta.com/oauth2/XXXXXXXXXXXXX",
client_id: "XXXXXXXXXXXXX",
redirect_uri: "https://localhost:4200/auth-callback",
post_logout_redirect_uri: "https://localhost:4200",
response_type: "code",
scope : "openid profile email"
}
}
export const environment = new EnvironmentImpl();
View Model
export abstract class EnvironmentViewModel {
abstract readonly production: boolean;
abstract readonly openID: IOpenIdViewModel;
}
export declare interface IOpenIdViewModel {
authority?: string;
client_id?: string;
redirect_uri?: string;
response_type?: string;
scope?: string;
silent_redirect_uri?: string;
post_logout_redirect_uri?: string;
filterProtocolClaims?: boolean
loadUserInfo?: boolean
automaticSilentRenew?: boolean;
monitorSession?: boolean;
accessTokenExpiringNotificationTime?: number;
checkSessionInterval?: number;
silentRequestTimeout?: number;
}
In the Module
import { environment } from 'src/environments/environment';
providers: [
{ provide: EnvironmentViewModel, useValue: environment }
]
tsconfig.lib.json
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es2015",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"includes": ["../../src/environments/environment.ts"],
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"lib": [
"es2018",
"dom"
],
"paths": {
"@falcon/core": [
"dist/@falcon-ng/core"
]
}
},
"angularCompilerOptions": {
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
The error message persists as:
ERROR: projects/falcon-core/src/lib/falcon-core.module.ts:28:29 - error TS6059: File '/Users/macbook/Projects/project-falcon/src/environments/environment.ts' is not under 'rootDir' '/Users/macbook/Projects/project-falcon/projects/falcon-core/src'. 'rootDir' is expected to contain all source files.
28 import { environment } from 'src/environments/environment';
For more information, refer to https://github.com/nrwl/nx/issues/208