The node module should not read from the environment file directly. There is no guarantee what file structure the user's environment files are in, or if they even have environment files. So the node_module should be totally agnostic to environements and instead you should pass in a variable when you import the module. This gives the application (and it's environment files) full control over what the node_module uses.
// app.module.ts
import { environment } from './environments/environment';
@NgModule({
imports: [MyNodeModule.setup(environment.myVar)]
})
export class AppModule {}
This allows the user to either pass in a static value or a dynamic value from their own environment files.
Update
To make the passed in value available to your node_module you can use an InjectionToken
export const MY_TOKEN = new InjectionToken<string>('My passed in token');
In your MyModule
you provide this token and assign it the value that was passed in.
@NgModule()
export class MyNodeModule {
static setup(myToken: string) {
return {
ngModule: MyNodeModule,
providers: [{ provide: MY_TOKEN, useValue: myToken }]
}
}
}
Now for the rest of your node_module to use the token, you can inject it.
@Injectable()
export class SomeService {
constructor(@Inject(MY_TOKEN) private myToken){}
}
Here is a very basic stackblitz demoing this functionality.