Closed paulschreiber closed 1 month ago
Yup this is a valid report as the code to fire this warning lives here: https://github.com/facebook/react-native/blob/87692454774d797cd3d7f26a0c31d53a4c90cfdd/packages/community-cli-plugin/src/utils/loadMetroConfig.js#L83-L109
@huntie do you know what might be going on here?
Interesting. @paulschreiber Your Metro config extends expo/metro-config
(rather than @react-native/metro-config
), and is being consumed via React Native Community CLI (rather than Expo CLI which would usually be the target for expo/metro-config
). This will be the cause.
Resolution: I'd suggest copying the template package.json
exactly, and then adding back either 1/ parts of expo/metro-config
as necessary, or 2/ all of expo/metro-config
and placing it 2nd into the mergeConfig
call (shown).
- const { getDefaultConfig } = require('expo/metro-config');
+ const { getDefaultConfig: getExpoDefaultConfig } = require('expo/metro-config');
- const { mergeConfig } = require('@react-native/metro-config');
+ const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {};
- module.exports = mergeConfig(getDefaultConfig(__dirname), config);
+ module.exports = mergeConfig(getDefaultConfig(__dirname), getExpoDefaultConfig(__dirname), config);
As for whether the warning should happen in this case, if extending expo/metro-config
only — it's correct, but not ideal. We designed the check to be robust by settting a global __REACT_NATIVE_METRO_CONFIG_LOADED
variable, specific to @react-native/metro-config
, with the expectation that this should be loaded in every user's config that inputs to CLI.
The warning will also be removed in future once we are confident people have migrated, likely 0.75.
cc @byCedric — Relevant to decision around expo/metro-config
not directly extending @react-native/metro-config
itself. We can review this, but it's not necessarily the direction we have to go. The above user fix is the valid approach.
@huntie As noted in steps to reproduce, this config file was created by the React Native and Expo scaffolding.
It's unclear from your comment who should make the changes to metro.config.js
(I assume package.json
is a typo).
@paulschreiber Right, this is part of Expo's instructions with npx install-expo-modules@latest
, I see now 👍🏻.
https://docs.expo.dev/bare/installing-expo-modules/#optional-use-expo-cli-for-bundling
I'll follow up with @byCedric.
Where is the React native CLI solution ?
Just going to put this here because it gave me a big headache when trying to integrate expo-doctor into our github ci actions. We had an issue with bundling/transforming where we had some redux actions being imported like so:
import * as actions from './someActionsFile'
and using redux's connect function we were using those actions to connect to props like so:
export default connect(mapStateToProps, {someAction: actions.someAction}, someComponent)
For some reason when we used only getDefaultConfig
from expo/metro-config
the actions.someAction
was undefined at call time on initial load, BUT if you saved the file to trigger a HMR it was now defined as expected. After changing our metro.config.js
to look like this it fixed the problem:
const { getDefaultConfig: getExpoDefaultConfig } = require('expo/metro-config');
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const config = {
transformer: {
assetPlugins: ['expo-asset/tools/hashAssetFiles']
}
};
module.exports = mergeConfig(
getExpoDefaultConfig(__dirname),
getDefaultConfig(__dirname),
config
);
This was in a bare react-native project that was around before expo became so popular so when we added expo I could've missed something in the documentation, but this was definitely a tricky bug.
@huntie which PR resolved the issue?
@paulschreiber As far as I can see, this is a user-space config issue. There are now workaround snippets above demonstrating the correct mergeConfig
expression.
Just going to put this here because it gave me a big headache when trying to integrate expo-doctor into our github ci actions. We had an issue with bundling/transforming where we had some redux actions being imported like so:
import * as actions from './someActionsFile'
and using redux's connect function we were using those actions to connect to props like so:
export default connect(mapStateToProps, {someAction: actions.someAction}, someComponent)
For some reason when we used only
getDefaultConfig
fromexpo/metro-config
theactions.someAction
was undefined at call time on initial load, BUT if you saved the file to trigger a HMR it was now defined as expected. After changing ourmetro.config.js
to look like this it fixed the problem:const { getDefaultConfig: getExpoDefaultConfig } = require('expo/metro-config'); const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config'); /** * Metro configuration * https://facebook.github.io/metro/docs/configuration * * @type {import('metro-config').MetroConfig} */ const config = { transformer: { assetPlugins: ['expo-asset/tools/hashAssetFiles'] } }; module.exports = mergeConfig( getExpoDefaultConfig(__dirname), getDefaultConfig(__dirname), config );
This was in a bare react-native project that was around before expo became so popular so when we added expo I could've missed something in the documentation, but this was definitely a tricky bug.
Quick update. We ended up switching our metro config to this once we upgraded to expo 51 and RN 0.74.5:
const { getDefaultConfig: getExpoDefaultConfig } = require('expo/metro-config');
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
/**
* Metro configuration
* https://facebook.github.io/metro/docs/configuration
*
* @type {import('metro-config').MetroConfig}
*/
const expoConfig = getExpoDefaultConfig(__dirname);
const defaultConfig = getDefaultConfig(__dirname);
const config = {
transformer: {
...expoConfig.transformer,
...defaultConfig.transformer,
assetPlugins: ['expo-asset/tools/hashAssetFiles']
}
};
module.exports = mergeConfig(defaultConfig, expoConfig, config);
The warning you're encountering suggests that your Metro config should extend @react-native/metro-config
to avoid build failures. To ensure compatibility, update your metro.config.js
as per the React Native template. For more on optimizing configurations for projects like topcelebrite.fr, follow the React Native setup guidelines closely.
Description
When I start my app locally (
npm run ios
), I get this error:My
metro.config.js
looks like this:Since I am using Expo, I need to extend that, too.
Steps to reproduce
npx install-expo-modules@latest
npm run ios
React Native Version
0.73.4
Affected Platforms
Build - MacOS
Output of
npx react-native info
Stacktrace or Logs
Reproducer
https://github.com/paulschreiber/metro-config-warning
Screenshots and Videos
No response