-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Built apps dont have env variables available. They are available when the app is in development #139
Comments
In order to include special files into the packaged application, you have to include them in the |
@bennymeg I added the .env path to maker.config.json but then too its not getting loaded. I was wondering how is it that I am getting all the variables in development mode but not when building the app. and also process.env.NODE_ENV is always development. |
Verify that the relative path of the '.env' file to the 'main.js' file inside the '.asar' file and in the unpackaged project are the same. |
Well, Its not getting shown there. Finally what i did was there was no secrets in the env's I moved them all to environment.ts files for all environment and change them during the building process with fileReplacements in the workspace.json I am not using .env's any more |
I have the same issue, with a single
Also I can't figure out how to do what you say here @bennymeg |
@Luca8991 Only transpiled code files and the assets folder get packaged (in a specific structure).
|
@bennymeg I read that document and tried to put the |
I managed to do it using a custom-webpack config in these steps:
Now the env variables are directly exposed in the webpack bundle, and the cool thing is that the only the variables exposed are the ones explicitly referenced in your code! Hope this can help also @aravindjaimon |
If it only contains environment variables it would be considered as bed practice, if it also contains secrets it would be also a bed idea. I most say, even if you use webpack to inject some of the variables into your code you should be careful, because, to my understanding, they will get bundled into your production code. |
Adding a comment here for any other folks that may come across this issue - I looked into NX convention is to automatically replace any environment variables that begin with The docs show an example of how to implement this for Angular applications, so I attempted to follow that pattern for electron but the example had a number of issues. Here is a working example that will replace any environment variables that are accessed using this pattern: This solution worked better for me since I only wanted to replace a limited number of environment variables and not normal node process environment variables that may be accessed at runtime.
{
...
"targets": {
"build": {
...
"options": {
...
"webpackConfig": "apps/electron/electron-app/webpack.config.js"
},
... You can alternatively use
const { DefinePlugin } = require('webpack');
const getWebpackConfig = require('@nrwl/react/plugins/webpack');
function getClientEnvironment() {
// Grab NODE_ENV and NX_* environment variables and prepare them to be
// injected into the application via DefinePlugin in webpack configuration.
const NX_APP = /^NX_/i;
const raw = Object.keys(process.env)
.filter((key) => NX_APP.test(key))
.reduce((env, key) => {
env[key] = process.env[key];
return env;
}, {});
// Stringify all values so we can feed into webpack DefinePlugin
return {
'process.env': Object.keys(raw).reduce((env, key) => {
env[key] = JSON.stringify(raw[key]);
return env;
}, {}),
};
}
module.exports = (config) => {
config = getWebpackConfig(config);
config.plugins = config.plugins || [];
config.plugins.unshift(new DefinePlugin(getClientEnvironment()));
return config;
}; |
Describe the bug
A clear and concise description of what the bug is.
Our app is running in two environments, production and staging. There is an env file that consists of different variables that need to be loaded. all the env's are available when the app is running in development mode
Expected behavior
A clear and concise description of what you expected to happen.
I am supposed to get the env's in the .env file when I try to get them via process.env
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: