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
Environment Variables not available in index.html for webpack 5 builds (React) #6941
Comments
If there is a workaround for this in the meantime, it would be greatly appreciated as it is currently blocking us. |
I'm not really sure how to fix the issue. But we created a workaround for our project since we needed some conditional rendering in our
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = (config, context) => {
const template = context.buildOptions?.index ?? context.options?.index;
// Remove the current html plugin added by NX
let idx = config.plugins.findIndex((plugin) => plugin.constructor.name === 'IndexHtmlWebpackPlugin');
config.plugins.splice(idx, 1);
// Push our own version with ejs template syntax enabled
config.plugins.push(new HtmlWebpackPlugin({ template: path.join(process.cwd(), template) }));
return config;
};
{
// ...
"build": {
"executor": "@nrwl/web:build",
"outputs": ["{options.outputPath}"],
"options": {
// ...
"index": "apps/admin/src/index.html", // <- important
"webpackConfig": "apps/admin/webpack.config.js", // <- important
"generateIndexHtml": false, // <- important
},
// ...
},
} With this you can use ejs template stuff in your index.html file: <% if (process.env.NODE_ENV === "production" && process.env.NX_SEGMENT_KEY) { %>
<script>
analytics.load(<%= JSON.stringify(process.env.NX_SEGMENT_KEY) %>);
</script>
<% } %> |
@adambrgmn Hi,I had this issue for webpack4 too. But I did not resolve it. Wish to get some ideas from you. Thanks. First, I changed to use HtmlWebpackPlugin as above. It did not work. Because NX built twice every time. The first time for esm,the built output is right, the variables are replaced. But after the twice build was over, the output is wrong. Like this: So, I tried some ways to prevent building twice. I tried to set |
@verymuch Hm, sorry, but I don't really know what's happening here 🤷♂️ We don't do multiple outputs. |
Is there any update? |
@FrozenPandaz can you please take a look? We observe same with process.env variables used in tsx for react projects, .env is just ignored for Webpack5 projects. |
any update on this? We're also blocked on this |
add another blocked dev to the list |
Also I don't think this is limited to react, I am using angular and environmental variables are not working for me either |
Can someone provide a reproduction of this issue? I have an example for Nx 13 and Nx 14, and both are working. Nx 13 - https://github.com/jaysoo/nx-13-env-vars If you are running into issues, please make sure you are on the latest versions ( |
@jaysoo |
Hi @neilsoult , thanks for the repro. The problem right now is that |
We decided not to add the same support to Angular for now since we don't control the executor/builder. However, I am updating the guide to include some workarounds. Preview: https://nx-dev-git-fork-jaysoo-docs-env-vars-nrwl.vercel.app/guides/environment-variables#using-environment-variables-in-angular-applications Since it sounds like an Angular-specific issue, I'm going to close this once the docs are updated. If you want the same support for Angular, please open a new enhancement issue and we can consider adding it. |
The guide is updated now: https://nx.dev/guides/environment-variables Closing this as the React/Web setup is working, and the guide above has some workaround for Angular for those interested. Again, open a new issue if you want to add similar support to Angular out of the box. |
Thank you for investigating this @jaysoo! |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Current Behavior
In the NX docs, it states that you can access the environment variables in the
index.html
file like so:https://nx.dev/latest/react/guides/environment-variables#using-environment-variables-in-indexhtml
For a react app that uses webpack 5, this is not the case. It does not replace the placeholder with the environment variable in the
.env
file.Expected Behavior
The environment variables should be injected into the index.html folder at build time.
Steps to Reproduce
npx create-nx-workspace@latest
.env
file with a variable in e.g.NX_TEST_VARIABLE='xyz'
index.html
file add the following tag to the body:<p>%NX_TEST_VARIABLE%</p>
nx serve app
- observe that the env variable is populated on the screennpx nx g @nrwl/web:webpack5
nx serve app
again - observe that now the env var is not populated and you see:%NX_TEST_VARIABLE%
insteadFailure Logs
No visible failure logs
Environment
NX report:
Node : 14.17.3
OS : darwin x64
npm : 7.20.0
nx : Not Found
@nrwl/angular : Not Found
@nrwl/cli : 12.8.0
@nrwl/cypress : 12.8.0
@nrwl/devkit : 12.8.0
@nrwl/eslint-plugin-nx : 12.8.0
@nrwl/express : Not Found
@nrwl/jest : 12.8.0
@nrwl/linter : 12.8.0
@nrwl/nest : Not Found
@nrwl/next : Not Found
@nrwl/node : Not Found
@nrwl/nx-cloud : Not Found
@nrwl/react : 12.8.0
@nrwl/schematics : Not Found
@nrwl/tao : 12.8.0
@nrwl/web : 12.8.0
@nrwl/workspace : 12.8.0
@nrwl/storybook : 12.8.0
@nrwl/gatsby : Not Found
typescript : 4.3.5
The text was updated successfully, but these errors were encountered: