react-native-builder-bob icon indicating copy to clipboard operation
react-native-builder-bob copied to clipboard

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

Open Alexufo opened this issue 7 months ago โ€ข 2 comments

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

  • [x] create-react-native-library
  • [ ] react-native-builder-bob

Selected options

latest

Link to repro

/

Environment

windows 10

Alexufo avatar Jun 04 '25 23:06 Alexufo

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;
})();

Alexufo avatar Jun 05 '25 00:06 Alexufo

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

vpodolyan avatar Jun 23 '25 10:06 vpodolyan

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

raahimkhan avatar Jul 03 '25 20:07 raahimkhan

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

Alexufo avatar Jul 03 '25 20:07 Alexufo

@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!!

raahimkhan avatar Jul 03 '25 20:07 raahimkhan

@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!

vikrant-kukadiya avatar Jul 08 '25 08:07 vikrant-kukadiya

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

Nodonisko avatar Jul 15 '25 21:07 Nodonisko

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

satya164 avatar Jul 20 '25 13:07 satya164

react-native-builder-bob version is to height, in my case, I create library with [email protected], 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...

John251314 avatar Sep 28 '25 13:09 John251314