facebook / metro

🚇 The JavaScript bundler for React Native
https://metrobundler.dev
MIT License
5.18k stars 614 forks source link

Reached heap limit Allocation failed - JavaScript heap out of memory "react-native": "0.75.3" #1355

Open Shperung opened 6 days ago

Shperung commented 6 days ago

Maybe it's bug. But I'm not sure.

I have updated my ReactNavite from "react-native": "0.72.12", to "react-native": "0.75.3".

On version 0.75.3 I use dynamic imports modules and use React lazy/suspense components. If I load ~ more than 10 dynamic imports, I see long loading each imports and than <--- JS stacktrace ---> FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory.

Same case was working good on "react-native": "0.72.12".

node v20.8.0

My relative dependencies

//...
    "react": "18.3.1",
    "@react-native/babel-preset": "0.75.3",
    "@react-native/eslint-config": "0.75.3",
    "@react-native/metro-config": "0.75.3",
    "@react-native/typescript-config": "0.75.3",
    "babel-preset-react-native": "4.0.1",
    "react-native-svg-transformer": "1.3.0",
    "react-native": "0.75.3",
//...

My metro.config.js

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

const defaultConfig = getDefaultConfig(__dirname);
const { assetExts, sourceExts } = defaultConfig.resolver;

const config = {
  transformer: {
    enableBabelRCLookup: false,
    babelTransformerPath: require.resolve('react-native-svg-transformer'),
  },
  resolver: {
    assetExts: assetExts.filter(ext => ext !== 'svg'),
    sourceExts: [...sourceExts, 'svg', 'mjs'],
  },
};

module.exports = mergeConfig(defaultConfig, config);
efstathiosntonas commented 4 days ago

Try running with:

node --max_old_space_size=16000 node_modules/react-native/cli.js start --reset-cache" for react native >= 0.75.x

node --max_old_space_size=16000 node_modules/react-native/local-cli/cli.js start --reset-cache for react-native < 0.75.x

Adjust 16000 based on your machine RAM, it's up to you if you want to keep the --reset-cache or not.

Shperung commented 2 days ago

Thank you. It's work for me