Metro: Unable to resolve module `..\\__generated__\\...` in React Native on Windows with Relay artifactDirectory option

Created on 13 Oct 2020  路  4Comments  路  Source: facebook/metro

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Error module not found

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.
Related to https://github.com/facebook/relay/issues/3201, but thought I should post here because it may be specifically a Metro issue.

OS: Windows 10
Node: v12.18.3
react: 16.13.1
react-native: 0.63.3
react-relay: 10.0.1

Reproduction repository using the SWAPI server from here https://github.com/graphql/swapi-graphql.

https://github.com/benmurden/react-native-relay-windows-repro

This only happens when using the artifactDirectory option in Relay, and only when bundled with React Native i.e. Metro bundler; this all works in React Web.

Sorry, not sure how this one could be tested with yarn test. To reproduce:

  • Run the SWAPI server
  • Clone the repro repo
  • Change the relay.environment.js to match the port given by SWAPI server
  • yarn install && yarn relay && yarn android

image

Looks like a Windows directory issue, since the bundler seems to have prepended too many upward directory traversals (..\\..\\..\\).

What is the expected behavior?
App builds and runs on Windows 10 and Android.

Windows

Most helpful comment

@RichardLindhout Thanks! I was inspired to create a PR that will not only resolve the above issue, but also fix current directory relative imports (.\\).

648

All 4 comments

Do you have a workaround available?

The isRelativeImport does not work on Windows it sees ..\generated not as relative while it is ;)

function isRelativeImport(filePath: string) {
  return /^[.][.]?(?:[/]|$)/.test(filePath);
}

We fixed it by adding

function isRelativeImport(filePath: string) {
   if (filePath.startsWith('..\\') {
      return true;
  }
  return /^[.][.]?(?:[/]|$)/.test(filePath);
}

@benmurden thanks for pointing into the right direction!

@RichardLindhout Thanks! I was inspired to create a PR that will not only resolve the above issue, but also fix current directory relative imports (.\\).

648

Was this page helpful?
0 / 5 - 0 ratings