question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot find documentation for metro.config.js > resolver > resolveRequest

See original GitHub issue

I cannot find any docs for the resolveRequest function listed at https://facebook.github.io/metro/docs/configuration#resolverequest

I implemented the function and inspected it with console.log(arguments), but when I return a string, it throws a type error.

resolveRequest(context, module, platform){
      let res = require.resolve(path.resolve(context.originModulePath, module));
      console.log(res);
      return res;
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
robhogancommented, Mar 5, 2022

Ah I see - if you init a new React Native app today you’ll get react-native@0.67.2 (latest stable) which includes metro@0.66.2, so the metro docs prior to 0.68 apply. Unfortunately, we don’t yet have a versioned docs website for metro, but you can access the raw docs at that point from github - https://github.com/facebook/metro/tree/v0.66.2/docs.

Prior to metro 0.68, context.resolveRequest points directly back to your own custom resolver, rather than the default resolver - so calling it from your own resolver creates a recursive loop which eventually throws, gets caught internally and manifests as a failed resolution. To access the default resolver you do need to use metro-resolver and ensure that you unset resolveRequest when passing the context, so the last thing you try is almost correct. The critical difference is that, in <0.68, the resolver takes a fourth argument, “realModuleName”.

So in summary, these are equivalent:

For Metro >=0.68

// metro.config.js

module.exports = {
  resolver: {
    resolveRequest: (context, moduleName, platform) => {
      // For example: redirecting `require('path')` to your own mock
      if (moduleName === 'path') {
        return {
          filePath: __dirname + '/my-mock-path.js',
          type: 'sourceFile',
        };
      }
      
      // Forward everything else to the default resolver
      return context.resolveRequest(
        context,
        moduleName,
        platform,
      );
    },
  },
};

For Metro < 0.68

// metro.config.js

const defaultResolver = require('metro-resolver').resolve;

module.exports = {
  resolver: {
    resolveRequest: (context, moduleName, platform, realModuleName) => {
      // For example: redirecting `require('path')` to your own mock
      if (moduleName === 'path') {
        return {
          filePath: __dirname + '/my-mock-path.js',
          type: 'sourceFile',
        };
      }

      // Forward everything else to the default resolver
      return defaultResolver(
        {
          ...context,
          resolveRequest: null,
        },
        moduleName,
        platform,
        realModuleName,
      );
    },
  },
};

Hope that helps, and sorry for the docs confusion.

5reactions
Niryocommented, Mar 14, 2021

There isn’t any useful docs for how to use the resolveRequest prop for module aliasing, so I played around with it and this is what I came up with:

    resolveRequest: (context, realModuleName, platform, moduleName) => {
      let module = realModuleName;
        if(realModuleName.includes('bla') || moduleName.includes('bla')) {
          module = module.replace('bla', 'blamos');
        }
        const { resolveRequest: removed, ...restContext } = context;
        return require("metro-resolver").resolve(restContext, module, platform);
    },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuring Metro - Meta Open Source
A Metro config can be created in these three ways (ordered by priority):. metro.config.js; metro.config.json; The metro field in package.json.
Read more >
How can I configure metro to resolve modules outside of my ...
2) Check `blockList` in your metro. config. js and make sure it isn't excluding the file path. The potential causes do not apply...
Read more >
@rnx-kit/metro-resolver-symlinks - npm
Start using @rnx-kit/metro-resolver-symlinks in your project by running `npm i ... resolveRequest in your metro.config.js :.
Read more >
@lomray/metro-custom-path-resolver - npm package | Snyk
Ensure you're using the healthiest npm packages. Snyk scans all the packages in your projects for vulnerabilities and provides automated fix advice. Get...
Read more >
How to import files from outside of root directory with React ...
...code, metro.config.js, rn-cli.config.js, etc. ... But this doesn't work. ... https://facebook.github.io/metro/docs/en/configuration#resolver-options.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found