shared.js exports incorrectly and no longer copies process.env variables
- [x] I understand that GitHub issues are not for tech support, but for questions specific to this generator, bug reports, and feature requests.
| Item | Version |
|---|---|
| generator-angular-fullstack | 5.0.0-beta.3 |
| Node | 9.8.0 |
| npm | 5.6.0 |
| Operating System | Windows 10 |
| Item | Answer |
|---|---|
| Transpiler | TypeScript |
| Markup | HTML |
| CSS | SCSS |
| Router | ? |
| Client Tests | Mocha |
| DB | MongoDB |
| Auth | Y |
When accessing constants like so
import constants from './app.constants';
The object is defined with this structure:
defaults: {
env:
etc"
}
Within app.module.ts an if statement appears which ads an injectable to the app (I don't understand what it does)
if (constants.env === 'development') {
@Injectable()
class HttpOptions extends BaseRequestOptions {
merge(options: RequestOptionsArgs): RequestOptions {
options.url = `http://localhost:9000${options.url}`;
return super.merge(options);
}
}
providers.push({ provide: RequestOptions, useClass: HttpOptions });
}
The if statement always fails as constants.env is always undefined. The correct way to access the variable would be with constants.default.env
Within server/config/environment/shared.js the export appears as module.exports.default and I have changed this to remove the default. Now my constants object can be accessed as above (constants.env)
This works fine, however I now have a CORS error when I run my app:
CORS header ‘Access-Control-Allow-Origin’ missing
I believe this is due to the Injectable code within app.module.ts (commenting out the code resolves the issue).
Finally, while testing shared.js I have noticed that it does not correctly import any process.env variables, and it seems the gulpfile is missing the task to copy them across. What is the appropriate way currently to get environment variables to the client side?
I now do this:
export const env = process.env.NODE_ENV;
export const port = process.env.PORT || 9000;
// List of user roles
export const userRoles = ['guest', 'user', 'admin'];
export default {
env,
port,
userRoles,
};
that seems to work...