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

expo-router + custom metro.config.js (resolveRequest) #501

Closed
romantseg opened this issue Apr 18, 2023 · 4 comments
Closed

expo-router + custom metro.config.js (resolveRequest) #501

romantseg opened this issue Apr 18, 2023 · 4 comments
Labels
Metro The issue is related to Metro bundler

Comments

@romantseg
Copy link

romantseg commented Apr 18, 2023

Summary

It seems that expo-router overrides resolveRequest in metro.config.js, which is needed for some packages (in my case it's graphql-request

I confirmed that without expo-router this works as expected, but after configuring expo-router, I keep getting Unable to resolve "graphql-request" from "index.js". Something is going on with how metro.config is resolved, but I can't pin point where

Minimal reproducible example

metro.config.js

I tried 2 different ways

// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require('expo/metro-config');

const config = getDefaultConfig(__dirname);

config.resolver = {
    ...config.resolver,
    resolveRequest: (context, moduleName, platform) => {
        if (moduleName.startsWith('graphql-request')) {
          return {
            filePath: `${__dirname}/node_modules/graphql-request/build/esm/index.js`,
            type: 'sourceFile',
          }
        }
    
        return context.resolveRequest(context, moduleName, platform)
      }
}

module.exports = config
module.exports = (async () => {
  const config = await getDefaultConfig(__dirname);
  const { resolver } = config;
  
  config.resolver = {
    ...resolver,
    resolveRequest: (context, moduleName, platform) => {
      console.log('resolveRequest', moduleName)
        if (moduleName.startsWith('graphql-request')) {
          return {
            filePath: `${__dirname}/node_modules/graphql-request/build/esm/index.js`,
            type: 'sourceFile',
          }
        }
    
        return context.resolveRequest(context, moduleName, platform)
      }
  };

  return config;
})();

index.js (or any other file where something is imported from graphql-request)

// import this just as an example
import { request, gql } from 'graphql-request'
import "expo-router/entry";
@romantseg romantseg changed the title expo-router + resolveRequest expo-router + custom metro.config.js (resolveRequest) Apr 18, 2023
@romantseg
Copy link
Author

If someone comes across this, one workaround is to import from graphql-request by absolute path, something like

import { GraphQLClient } from "./node_modules/graphql-request/build/esm/index.js"
...

still would be great to resolve this at metro.config.js, as it works without expo-router

@EvanBacon EvanBacon added the Metro The issue is related to Metro bundler label Apr 18, 2023
@byCedric
Copy link
Member

byCedric commented Apr 19, 2023

Hi @RomanTsegelskyi, thanks for mentioning this! Unfortunately, Metro doesn't actually support ESM. It uses a Babel transformer to compile basic ESM back into CommonJS. Things should be way better once Metro "officially" supports ESM, which is currently in progress:

Until then, I'm afraid that you'd need to use ESM workarounds. But, preferably, use the actual source (since Metro transpile everything, including modules) or use a CommonJS version.

Hope this helps!

@romantseg
Copy link
Author

After some more experimentation, this is not expo-router issue, but a metro issue. If anyone finds it later, just used the workaround I describe

@beeman
Copy link

beeman commented May 11, 2023

If someone comes across this, one workaround is to import from graphql-request by absolute path, something like

import { GraphQLClient } from "./node_modules/graphql-request/build/esm/index.js"
...

still would be great to resolve this at metro.config.js, as it works without expo-router

This made it work for me! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Metro The issue is related to Metro bundler
Projects
None yet
Development

No branches or pull requests

4 participants