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

React Native Metro: Ignoring packages during dependency resolution #18612

Closed
enchorb opened this issue Aug 14, 2023 · 9 comments
Closed

React Native Metro: Ignoring packages during dependency resolution #18612

enchorb opened this issue Aug 14, 2023 · 9 comments
Assignees
Labels

Comments

@enchorb
Copy link

enchorb commented Aug 14, 2023

Description

Certain packages need to be ignored during dependency resolution for Metro (http, https, zlib), depending on your Node version.

The solution outlined here facebook/metro#519, does not work with Nx, I'm guessing because of merging it into the Nx Metro config which overrides it.

Motivation

Needed in some instances to build the app successfully

@AgentEnder AgentEnder added the scope: react-native Issues relating to React Native label Aug 15, 2023
@atarek12
Copy link

atarek12 commented Aug 20, 2023

I am facing the same problem while trying to install axios package. here is a quick reproduction repo

I have used these commands to build the repo

npx create-nx-workspace --preset=expo
npm install axios
yarn nx start undefiend

and then found this error error: Error: Cannot resolve http and it is being resolved when running yarn add [email protected]

to reproduce:

git clone https://github.com/atarek12/nx-expo-axios
cd nx-expo-axios
yarn install
yarn nx start undefiend

@lewisd1996
Copy link

Facing the same issue as @enchorb and @atarek12, sometimes it's http that cannot be resolved and sometimes http2 but the problems the same

@xiongemi
Copy link
Collaborator

in your app's metro.config.js, how about comment out the line: unstable_enablePackageExports: true,?

@david-gettins
Copy link

david-gettins commented Oct 17, 2023

@xiongemi Unfortunately that option causes issues with AWS Amplify. See here. It has already been disabled.

@xiongemi
Copy link
Collaborator

@david-gettins i think you can do something like this to override nx's resolve request:

const path = require("path");

const blacklistedModules = ["https", "http", "zlib"];

const nxConfig = withNxMetro(mergeConfig(defaultConfig, customConfig), {
  // Change this to true to see debugging info.
  // Useful if you have issues resolving modules
  debug: false,
  // all the file extensions used for imports other than 'ts', 'tsx', 'js', 'jsx', 'json'
  extensions: [],
  // Specify folders to watch, in addition to Nx defaults (workspace libraries and node_modules)
  watchFolders: [],
});

module.exports = mergeConfig(nxConfig, {
  resolve: {
    resolveRequest: (context, realModuleName, platform, moduleName) => {
      if (blacklistedModules.includes(moduleName)) {
        return {
          filePath: path.resolve(__dirname + "/src/shim-module.js"),
          type: "sourceFile"
        };
      } else {
        return nxConfig.resolve.resolveRequest(
          context, realModuleName, platform
        );
      }
    }
  }
})

@lewisd1996
Copy link

lewisd1996 commented Oct 20, 2023

Hi @xiongemi thank you for your suggestion, unfortunately it hasn't worked for me, this is the error specifically:

error: Error: Cannot resolve http at /Users/lewis/Documents/GitHub/information-to-live-by/node_modules/@nx/react-native/plugins/metro-resolver.js:30:15

here is the entire metro config:

const { withNxMetro } = require('@nx/react-native');
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const exclusionList = require('metro-config/src/defaults/exclusionList');

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

const path = require('path');

const blacklistedModules = ['https', 'http', 'zlib'];

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const customConfig = {
  transformer: {
    babelTransformerPath: require.resolve('react-native-svg-transformer'),
  },
  resolver: {
    assetExts: assetExts.filter((ext) => ext !== 'svg'),
    sourceExts: [...sourceExts, 'svg'],
    blockList: exclusionList([
      /^(?!.*node_modules).*\/dist\/.*/,
      /(.*(\.github).*)/,
    ]),
    unstable_enableSymlinks: true,
    unstable_enablePackageExports: false,
    extraNodeModules: {
      crypto: require.resolve('crypto-browserify'),
      stream: require.resolve('stream-browserify'),
    },
  },
};

module.exports = withNxMetro(mergeConfig(defaultConfig, customConfig), {  
  debug: false,
  extensions: [],  
  watchFolders: [],
  resolve: {
    resolveRequest: (context, realModuleName, platform, moduleName) => {
      if (blacklistedModules.includes(moduleName)) {
        return {
          filePath: path.resolve(__dirname + '/src/shim-module.js'),
          type: 'sourceFile',
        };
      } else {
        return customConfig.resolve.resolveRequest(
          context,
          realModuleName,
          platform,
        );
      }
    },
  },
});

@xiongemi
Copy link
Collaborator

xiongemi commented Nov 1, 2023

@lewisd1996 how about the nx config like

const { withNxMetro } = require('@nx/react-native');
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');
const exclusionList = require('metro-config/src/defaults/exclusionList');
const path = require('path');

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

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const customConfig = {
  transformer: {
    babelTransformerPath: require.resolve('react-native-svg-transformer'),
  },
  resolver: {
    assetExts: assetExts.filter((ext) => ext !== 'svg'),
    sourceExts: [...sourceExts, 'svg'],
    blockList: exclusionList([/^(?!.*node_modules).*\/dist\/.*/]),
    unstable_enableSymlinks: true,
    unstable_enablePackageExports: true,
  },
};

module.exports = withNxMetro(mergeConfig(defaultConfig, customConfig), {
  // Change this to true to see debugging info.
  // Useful if you have issues resolving modules
  debug: true,
  // all the file extensions used for imports other than 'ts', 'tsx', 'js', 'jsx', 'json'
  extensions: [],
  // Specify folders to watch, in addition to Nx defaults (workspace libraries and node_modules)
  watchFolders: [],
}).then((nxConfig) => {
  const blacklistedModules = ['https', 'http', 'zlib'];
  return mergeConfig(nxConfig, {
    resolver: {
      resolveRequest: (context, realModuleName, platform, moduleName) => {
        if (blacklistedModules.includes(moduleName)) {
          return {
            filePath: path.resolve(__dirname + '/src/shim-module.js'),
            type: 'sourceFile',
          };
        } else {
          return nxConfig.resolver.resolveRequest(
            context,
            realModuleName,
            platform
          );
        }
      },
    },
  });
});

@lewisd1996
Copy link

Hi @xiongemi thank you for getting back.

Turns out it's a combination of issues with nx, React Native and AWS. This issue explains more. Downgrading aws-sdk packages gets us by for now.

@xiongemi xiongemi closed this as completed Nov 6, 2023
Copy link

github-actions bot commented Dec 7, 2023

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants