metro: [0.80.11] breaks aliases with babel-plugin-module-resolver on imports (?)

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

bug, using react-native 0.75.2, Paper, watchman@2024.08.26.00

Tried: 1.rm -rf node_modules 2.--reset-cache 3. no luck

downgrading to 0.80.10 works as a charm.

What is the current behavior?

throws:

error: Error: Unable to resolve module ./ from /Users/stathis/IdeaProjects/xxxxxx/src/xxxxxx/posts/components/comments/PostComment/components/ContentCommentLines.tsx:

None of these files exist:
  * src/flows/posts/components/comments/PostComment/components/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|.android.svg|.native.svg|.svg)
  3 | import isEqual from 'react-fast-compare';
  4 |
> 5 | import {
    | ^
  6 |   ContentLeftBorderInnerView,
  7 |   ContentLeftBorderOuterView
  8 | } from '@flows/posts/components/comments/PostComment/components';
    at ModuleResolver.resolveDependency (/Users/stathis/IdeaProjects/xxxxxx/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:112:15)
Click for simulator screenshot

Screenshot 2024-09-04 at 16 03 01

Click for babel config
const moduleResolver = [
  [
    'module-resolver',
    {
      root: ['./src'],
      extensions: [
        '.ios.ts',
        '.android.ts',
        '.ts',
        '.ios.tsx',
        '.android.tsx',
        '.tsx',
        '.jsx',
        '.js',
        '.json'
      ],
      alias: {
        '@assets': './src/assets',
        '@components': './src/components',
        '@contexts': './src/contexts',
        '@flows': './src/flows',
        '@functions': './src/functions',
        '@generated': './src/generated',
        '@hooks': './src/hooks',
        '@store': './src/store',
        '@styles': './src/styles',
        '@themes': './src/themes',
        '@typings': './src/types',
        '@utils': './src/utils'
      }
    }
  ]
];

module.exports = {
  presets: [
    'module:@react-native/babel-preset'
  ],
  plugins:
    process.env.NODE_ENV === 'production'
      ? [
          'transform-remove-console',
          ...moduleResolver,
          'macros',
          'react-native-reanimated/plugin'
        ]
      : [...moduleResolver, 'macros', 'react-native-reanimated/plugin']
};

Click for tsconfig
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "allowUnreachableCode": false,
    "experimentalDecorators": true,
    "allowUnusedLabels": false,
    "noStrictGenericChecks": false,
    "esModuleInterop": true,
    "importHelpers": true,
    "isolatedModules": true,
    "jsx": "react-native",
    "lib": ["esnext"],
    "moduleResolution": "node",
    "noEmit": true,
    "module": "esnext",
    "paths": {
      "*": ["src/*"],
      "@assets/*": ["src/assets/*"],
      "@components/*": ["src/components/*", "components/*"],
      "@contexts/*": ["src/contexts/*"],
      "@flows/*": ["src/flows/*"],
      "@functions/*": ["src/functions/*"],
      "@generated/*": ["src/generated/*"],
      "@hooks/*": ["src/hooks/*"],
      "@store/*": ["src/store/*"],
      "@styles/*": ["src/styles/*"],
      "@themes/*": ["src/themes/*"],
      "@typings/*": ["src/types/*"],
      "@utils/*": ["src/utils/*"]
    },
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": true,
    "target": "esnext",
    "typeRoots": ["./typings"]
  },
  "exclude": [
    "node_modules",
    "./node_modules/",
    "**/node_modules",
    "ios",
    "android",
    "babel.config.js",
    "metro.config.js"
  ],
  "include": ["src", "__tests__", "typings"]
}

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.

What is the expected behavior?

expect to import packages with aliases

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

metro config:

const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config');

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

metroConfig = (() => {
  return {
    transformer: {
      babelTransformerPath: require.resolve('react-native-svg-transformer/react-native')
    },
    resolver: {
      assetExts: [...assetExts.filter((ext) => ext !== 'svg'), 'bin', 'lottie'],
      sourceExts: [...sourceExts, 'svg']
    }
  };
})();

module.exports = mergeConfig(defaultConfig, metroConfig);

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

Renaming the file or just moving app/utils.ts to app/utils/index.ts would work as long as I own the code, but can it happens with library code as well (I mean inside node_modules package).

Third-party npm packages are (I hope šŸ˜¬šŸ¤ž) not relying on Babel to transform their imports. If they ship an import * from './' expecting it to resolve to something in the parent directory, I believe that’d already be broken under Node and Webpack. Metro is aligning with others here - please correct me if I’m wrong.

Should we consider fixing babel-plugin-module-resolver by checking that an index.[suffixes] file exist before transforming to ā€œ./ā€ ?

A Babel plugin shouldn’t be performing any IO - that makes transform caching impossible - but I think if it transformed to . without the slash that would be fine. But yep fixing this in the plugin is the right way forward here.

Metro resolving ā€œ./ā€œ to ā€œā€¦/dirname.jsā€ was always a bug. We didn’t anticipate it affecting babel-plugin-module-resolver like this, but I don’t think we should revert the fix.

please feel free to close this issue

Yeah, I’m going to close this as a ā€œwon’t fixā€ - it’s a babel-plugin-module-resolver bug.

Thanks for the reports though folks - it’s good to know about this.

totally off topic, but do you have any idea why metro is jumping to 100% while it’s still obviously loading files? It happens on iOS only, simulator rn info bar shows ā€œgoodā€ percentages not 100% , on the video the ā€œnormalā€ percentage counting is for Android. This is happening for at least 7-8 metro versions now.

Screen.Recording.2024-09-05.at.09.33.13.mov

Thanks for the report - the progress bar is indicative at best (it never really knows how much of the graph is undiscovered) but moving to 100% immediately is a bug - seems like it’s racing such that instead of going from 0/1 to 1/n, it’s going to 1/1 after the first transform, and then by design it never goes backwards.

hey Rob, sorry for the late reply, yeah you’re right, need to get rid of this bad file naming šŸ˜‚, it’s confusing.

will try to fix it tomorrow and update the issue.

Ah, I missed that you were using a Babel module resolver.

We don’t support that, but we didn’t intend to break it. Let me try to repro this…

Thanks for the quick report @efstathiosntonas. Could you share how @flows/posts is set up for you? In particular, is that a bare package.json or does it define exports? (I’m guessing there’s a package.json at src/flows/posts or src/flows?

Also, what file should this be resolving to? Is it included within the files Metro is claiming don’t exist?