Skip to content

require() of ES Module from awesome-library\example\metro.config.js not supported. #858

Closed as not planned

Description

@Alexufo

Description

npx create-react-native-library@latest awesome-library2
√ What is the name of the npm package? ... react-native-awesome-library2
....
√ What type of library do you want to develop? » Turbo module
✔ Project created successfully at awesome-library2!

  1. npx create-react-native-library@latest awesome-library2

  2. npx react-native start inside example folder

E:\awesome-library2\example>yarn react-native start
error require() of ES Module E:\awesome-library2\example\node_modules\react-native-monorepo-config\index.js from E:\awesome-library2\example\metro.config.js not supported.
Instead change the require of index.js in E:\awesome-library2\example\metro.config.js to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module E:\awesome-library2\example\node_modules\react-native-monorepo-config\index.js from E:\awesome-library2\example\metro.config.js not supported.
Instead change the require of index.js in E:\awesome-library2\example\metro.config.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (E:\awesome-library2\example\metro.config.js:3:29)
    at module.exports (E:\awesome-library2\example\node_modules\cosmiconfig\node_modules\import-fresh\index.js:28:9)
    at loadJs (E:\awesome-library2\example\node_modules\cosmiconfig\dist\loaders.js:9:18)
    at Explorer.loadFileContent (E:\awesome-library2\example\node_modules\cosmiconfig\dist\createExplorer.js:230:12)
    at E:\awesome-library2\example\node_modules\cosmiconfig\dist\createExplorer.js:252:21

Packages

  • create-react-native-library
  • react-native-builder-bob

Selected options

latest

Link to repro

/

Environment

windows 10

Activity

  1. Alexufo commented on Jun 5, 2025

    @Alexufo
    Author

    metro.config.js
    helped...

    const path = require('path');
    const { getDefaultConfig } = require('@react-native/metro-config');
    const root = path.resolve(__dirname, '..');
    
    /**
     * Metro configuration
     * https://facebook.github.io/metro/docs/configuration
     *
     * @type {import('metro-config').MetroConfig}
     */
    module.exports = (async () => {
      const { withMetroConfig } = await import('react-native-monorepo-config'); 
      const defaultConfig = await getDefaultConfig(__dirname);
      
      const config = withMetroConfig(defaultConfig, {
        root,
        dirname: __dirname,
      });
      config.resolver.unstable_enablePackageExports = true;
      return config;
    })();
  2. changed the title [-]require() of ES Module from E:\awesome-library2\example\metro.config.js not supported.[/-] [+]require() of ES Module from awesome-library\example\metro.config.js not supported.[/+] on Jun 9, 2025
  3. vpodolyan commented on Jun 23, 2025

    @vpodolyan

    @Alexufo thank you for the fix! It'd be great to update the template

  4. raahimkhan commented on Jul 3, 2025

    @raahimkhan

    The error was gone after using the code provided by Alexufo above. However, upon running, I am still getting this error. I added no new code at this point. Just used the command mentioned in the docs and trying to run the example code. Can anyone help please.

    Image
  5. Alexufo commented on Jul 3, 2025

    @Alexufo
    Author

    @raahimkhan please create new empty project from react-native-builder-bob and compare package.json with yours. Its the same?

  6. raahimkhan commented on Jul 3, 2025

    @raahimkhan

    @raahimkhan please create new empty project from react-native-builder-bob and compare package.json with yours. Its the same?

    I create a fresh one and it is working now. Weird though because previous one was also fresh :) Maybe some cache or other issue. Thanks though!!

  7. vikrant-kukadiya commented on Jul 8, 2025

    @vikrant-kukadiya

    @Alexufo @raahimkhan @vpodolyan Thanks for the helpful direction! I was able to get things working based on your suggestion, with a slight adjustment for my Expo setup.

    In case it helps others using Expo in a monorepo, here's the Metro config that worked for me:

    const path = require('path');
    const { getDefaultConfig } = require('@expo/metro-config');
    const root = path.resolve(__dirname, '..');
    
    /**
     * Metro configuration
     * https://facebook.github.io/metro/docs/configuration
     *
     * @type {import('metro-config').MetroConfig}
     */
    module.exports = (async () => {
      const { withMetroConfig } = await import('react-native-monorepo-config');
      const defaultConfig = await getDefaultConfig(__dirname);
    
      const config = withMetroConfig(defaultConfig, {
        root,
        dirname: __dirname,
      });
    
      config.resolver.unstable_enablePackageExports = true;
      return config;
    })();

    The key change for me was switching from @react-native/metro-config to @expo/metro-config since I'm working with an Expo-managed workflow. Everything else aligns closely with your setup.

    Thanks again!

  8. Nodonisko commented on Jul 15, 2025

    @Nodonisko

    I created fresh library using npx create-react-native-library@latest and I am getting this error when I try to run example app.

  9. satya164 commented on Jul 20, 2025

    @satya164
    Member

    You are getting the error because you're on an old version of Node.js. See the .nvmrc in your generated project for the Node.js version supported by the project.

    Alternatively, you should be able to use any of the following versions of Node.js

    • v20.19.0 and higher (LTS)
    • v22.12.0 and higher (LTS)
    • v23.4.0 and higher
  10. John251314 commented on Sep 28, 2025

    @John251314

    react-native-builder-bob version is to height, in my case, I create library with create-react-native-library@0.49.8, and the react-native-builder-bob version is 0.43.13. When i run command with NodeJS 23.4, it success! By the way, this package is so hard to use...

  11. pedrol2b commented on Jan 4, 2026

    @pedrol2b

    You are getting the error because you're on an old version of Node.js. See the .nvmrc in your generated project for the Node.js version supported by the project.

    Alternatively, you should be able to use any of the following versions of Node.js

    * v20.19.0 and higher (LTS)
    
    * v22.12.0 and higher (LTS)
    
    * v23.4.0 and higher
    
        "react-native": "0.81.1",
        "react-native-builder-bob": "^0.40.13",

    Can confirm than by doing this it worked for me
    for reference was using node v20.15.0 upgraded to v22.21.1 and worked fine

    Tip

    For anyone doing this remember to update your .xcode.env.local

    Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions