Skip to content
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

Unable to read env variables in metro.config.js #986

Open
Hardik3296 opened this issue May 18, 2023 · 0 comments
Open

Unable to read env variables in metro.config.js #986

Hardik3296 opened this issue May 18, 2023 · 0 comments

Comments

@Hardik3296
Copy link

Hardik3296 commented May 18, 2023

Do you want to request a feature or report a bug?
When I run the metro server in development mode and start it separately, I can easily pass the environment variables like

ENVFILE=.env.dev react-native start

I can then easily read this environment variable in meto.config.js and perform certain actions. But when I try to build the app from production the react-native server is started on its own by the run-ios or run-android command. I am passing the environment variable to these scripts as

ENVFILE=.env.prod react-native run-ios

I can easily access this value in the code, but when I try to access the same in metro.config.js it returns as undefined. I want to know if there is a way to pass the environment variables to metro server when creating the production application.

What is the current behavior?
Current behaviour seems to be that the environment variables are not passed to metro server, if it is not started separately.

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
None

What is the expected behavior?
Expected behaviour is that the environment variables passed to run-ios and run-android scripts should be forwarded to react-native start command as well.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

`
const { getDefaultConfig } = require("metro-config");
// const exclusionList = require("metro-config/src/defaults/exclusionList");

module.exports = (async () => {
const {
resolver: { sourceExts, assetExts },
} = await getDefaultConfig();

// List of folders and files to be excluded
// let listToBeExcluded = [/.test.js$/];
// if (!(process.env.NODE_ENV === "development")) {
// listToBeExcluded = [
// ...listToBeExcluded,
// /src/modules/NonProdOptions/.*/,
// ];
// }

return {
transformer: {
babelTransformerPath: require.resolve("react-native-svg-transformer"),
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: true,
},
}),
},
resolver: {
assetExts: assetExts.filter(ext => ext !== "svg"),
sourceExts: [...sourceExts, "svg"],
// blockList: exclusionList(listToBeExcluded),
},
maxWorkers: 2,
};
})();`

yarn: 1.22.19
node: v19.8.1
OS: macOS Vetura(13.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant