Open r3plica opened 7 months ago
Hello!
I was having the same issue trying to use env variables with module federation. What it worked for me is use nx webpack composePlugins for webpack-config
const { composePlugins } = require('@nx/webpack');
const { withModuleFederation } = require('@nx/angular/module-federation');
const moduleFederationConfig = require('./module-federation.config');
const webpack = require('webpack');
const dotenv = require('dotenv');
module.exports = composePlugins(
withModuleFederation(moduleFederationConfig),
config => {
config.plugins.push(new webpack.DefinePlugin(getProcessEnv()));
return config;
},
);
function getProcessEnv() {
return {
'process.env': JSON.stringify(dotenv.config().parsed),
};
}
This composePlugins config is for angular app. Here it is the NX documentation for this: https://nx.dev/recipes/webpack/webpack-plugins#nxenhanced-plugins
Hope this helps!
I'm having the same problem. This is because it's not working for remotes :(
The host process environment variables are processed like I'd expect. The remotes get the vars from the root and from the host/shell
Documentation issue
Is there a specific documentation page you are reporting?
Enter the URL or documentation section here.
Additional context or description
I have spent too long trying to get this to work. We are using module federation apps in our nx workspace along with libraries. I follow the documentation and added this to my
module-federation-config.js
I can confirm that when serve the application, the raw console.log is show correctly (although NODE_ENV) is undefined.
in my
app.component.ts
or any file in my libraries, if I addconsole.log(process.env);
I get an error stating:ReferenceError: process is not defined
I have tried so many things to get this to work. Every application and every library has a
tsconfig.app|lib.json
and atsconfig.spec.json
and all of them have:and in my package.json I have
"@types/node": "^16.11.7",
in the devDependencies, but it still doesn't work.