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

Dynamic imports do not respect watchFolders configuration #1269

Open
taylorkline opened this issue May 2, 2024 · 6 comments
Open

Dynamic imports do not respect watchFolders configuration #1269

taylorkline opened this issue May 2, 2024 · 6 comments

Comments

@taylorkline
Copy link

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

A bug

What is the current behavior?

Dynamic import fails to resolve:

Error: Unable to resolve module ./node_modules/react-native-url-polyfill/auto from .../packages/mobile/.:

None of these files exist:
  * node_modules/react-native-url-polyfill/auto(.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
  * node_modules/react-native-url-polyfill/auto/index(.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)

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.

  1. Initialize a React Native project
  2. Place it within a yarn v4 monorepo as a workspace
  3. Install react-native-url-polyfill in the react native workspace
    • Configure watchFolders to include ../../: watchFolders: [path.resolve(__dirname, "../../")],
  4. Ensure react-native-url-polyfill exists at ../../node_modules/react-native-url-polyfill
  5. Add dynamic import import("react-native-url-polyfill/auto")

I'm sorry, I do not have time to create a reproducible example repo right now.

What is the expected behavior?

Dynamic import should resolve successfully.

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

System:
  OS: macOS 14.4.1
  CPU: (12) arm64 Apple M3 Pro
  Memory: 151.80 MB / 18.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.20.2
    path: ~/Library/Caches/fnm_multishells/49824_1714674459208/bin/node
  Yarn:
    version: 4.1.1
    path: ~/Library/Caches/fnm_multishells/49824_1714674459208/bin/yarn
  npm:
    version: 10.5.0
    path: ~/Library/Caches/fnm_multishells/49824_1714674459208/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/taylor/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.4
      - iOS 17.4
      - macOS 14.4
      - tvOS 17.4
      - visionOS 1.1
      - watchOS 10.4
  Android SDK:
    API Levels:
      - "26"
      - "28"
      - "29"
      - "30"
      - "31"
      - "32"
      - "33"
      - "34"
    Build Tools:
      - 23.0.1
      - 25.0.2
      - 25.0.3
      - 27.0.3
      - 28.0.0
      - 28.0.3
      - 29.0.2
      - 29.0.3
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 33.0.0
      - 33.0.1
      - 34.0.0
      - 35.0.0
      - 35.0.0
    System Images:
      - android-21 | Google APIs Intel x86 Atom_64
      - android-29 | Intel x86 Atom_64
      - android-29 | Google APIs ARM 64 v8a
      - android-29 | Google Play Intel x86 Atom
      - android-30 | Google APIs Intel x86 Atom
      - android-30 | Google Play Intel x86 Atom
      - android-33 | Google APIs Intel x86_64 Atom
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.2 AI-232.10227.8.2321.11479570
  Xcode:
    version: 15.3/15E204a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 3.3.0
    path: /Users/taylor/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react: Not Found
  react-native: Not Found
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
@robhogan
Copy link
Contributor

robhogan commented May 2, 2024

Hmm - thanks for the report. That's unexpected and it's particularly surprising that dynamic imports should be any different here - does it work if you use a static require/import?

@taylorkline
Copy link
Author

Sure thing!

Hmm - thanks for the report. That's unexpected and it's particularly surprising that dynamic imports should be any different here - does it work if you use a static require/import?

Indeed it does, a simple

import "react-native-url-polyfill/auto";

up with the imports in index.js works fine where

import("react-native-url-polyfill/auto") does not.

Additionally it works fine if I prevent hoisting of react-native-url-polyfill or copy it into packages/mobile/node_modules.

@robhogan
Copy link
Contributor

robhogan commented May 2, 2024

That is quite bizarre! The resolution logic should be exactly the same afaik. Would you be able to share your metro.config.js (if not the whole thing, anything under resolver)? Is this an Expo project?

@taylorkline
Copy link
Author

Sure, it's fairly simple:

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require("path");

/**
 * Metro configuration
 * https://facebook.github.io/metro/docs/configuration
 *
 * @type {import('metro-config').MetroConfig}
 */
const config = {
  watchFolders: [path.resolve(__dirname, "../../")],
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

@taylorkline
Copy link
Author

Also, it is not an Expo project.

@Bob-FE
Copy link

Bob-FE commented May 7, 2024

The same issue was encountered when using dynamic import in version rn-0.74/73 of Monorepo, but it is normal in non Monorepo.
In 0.72, everything is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants