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

Unable to resolve module @babel/runtime/helpers/interopRequireDefault – without changing anything #27712

Closed
slhck opened this issue Jan 8, 2020 · 28 comments
Labels
Bug Platform: Linux Building on Linux. Stale There has been a lack of activity on this issue and it may be closed soon. Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used.

Comments

@slhck
Copy link

slhck commented Jan 8, 2020

React Native version:

System:
    OS: Linux 5.0 Ubuntu 19.04 (Disco Dingo)
    CPU: (8) x64 Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
    Memory: 1.99 GB / 31.28 GB
    Shell: 5.5.1 - /usr/bin/zsh
  Binaries:
    Node: 13.2.0 - ~/.nvm/versions/node/v13.2.0/bin/node
    Yarn: 1.19.2 - ~/.nvm/versions/node/v13.2.0/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v13.2.0/bin/npm
    Watchman: 4.9.0 - /home/linuxbrew/.linuxbrew/bin/watchman
  SDKs:
    Android SDK:
      API Levels: 28, 29
      Build Tools: 28.0.3
      System Images: android-28 | Google APIs Intel x86 Atom
  npmPackages:
    react: 16.9.0 => 16.9.0 
    react-native: 0.61.5 => 0.61.5 
  npmGlobalPackages:
    react-native-cli: 2.0.1

Steps To Reproduce

I opened a project that I've been working on for a few weeks, and I decided to run it after not having touched it for a few days:

  1. Run npx react-native start
  2. Run npx react-native run-android

I expected the project to start in Android. Instead, I got:

Looking for JS files in
   /foo 

Loading dependency graph, done.
 BUNDLE  [android, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1)::ffff:127.0.0.1 - - [08/Jan/2020:16:10:27 +0000] "GET /index.bundle?platform=android&dev=true&minify=false HTTP/1.1" 500 - "-" "okhttp/3.12.1"
error: bundling failed: Error: Unable to resolve module `@babel/runtime/helpers/interopRequireDefault` from `index.js`: @babel/runtime/helpers/interopRequireDefault could not be found within the project.

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules: rm -rf node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
    at ModuleResolver.resolveDependency (/foo/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:186:15)
    at ResolutionRequest.resolveDependency (/foo/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (/foo/node_modules/metro/src/node-haste/DependencyGraph.js:282:16)
    at Object.resolve (/foo/node_modules/metro/src/lib/transformHelpers.js:267:42)
    at /foo/node_modules/metro/src/DeltaBundler/traverseDependencies.js:426:31
    at Array.map (<anonymous>)
    at resolveDependencies (/foo/node_modules/metro/src/DeltaBundler/traverseDependencies.js:423:18)
    at /foo/node_modules/metro/src/DeltaBundler/traverseDependencies.js:275:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/foo/node_modules/metro/src/DeltaBundler/traverseDependencies.js:87:24)
 BUNDLE  [android, dev] ./index.js ░░░░░░░░░░░░░░░░ 0.0% (0/1), failed.

Of course, the file exists:

ls -l node_modules/@babel/runtime/helpers/interopRequireDefault.js
-rw-r--r-- 1 werner werner 147 Oct 26  1985 node_modules/@babel/runtime/helpers/interopRequireDefault.js

So I don't know why it wouldn't find it. Note that I have not changed anything in the project, and even reverting to an old commit from a few weeks ago does not solve this problem.

A colleague of mine has exactly the same project and can run it without problems. I have even tried to run a copy of his directory including the downloaded node_modules folder, and it fails.

I could also reproduce this on a fresh install of NVM and Node under macOS.

I could not reproduce this in a completely new RN project. But cloning my project into another folder and running npm install, the error persists.

Trying the following did not work:

watchman watch-del-all
rm -rf node_modules
rm -rf /tmp/metro-*
npm add @babel/runtime
npm i
npx react-native start --reset-cache

This is the package.json:

{
  "name": "foo",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint ."
  },
  "dependencies": {
    "async": "^2.1.2",
    "base-64": "^0.1.0",
    "body-parser": "^1.15.2",
    "colors": "^1.1.2",
    "cors": "^2.8.1",
    "crypto-js": "^3.1.5",
    "express": "^4.14.0",
    "localforage": "^1.4.3",
    "pako": "^1.0.3",
    "pem": "^1.8.3",
    "react": "16.9.0",
    "react-native": "0.61.5",
    "react-native-gesture-handler": "^1.5.2",
    "react-native-progress": "^4.0.2",
    "react-native-reanimated": "^1.4.0",
    "react-native-simple-gauge": "^0.2.2",
    "react-native-video": "^5.0.2",
    "react-native-video-controls": "^2.2.3",
    "react-navigation": "^4.0.10",
    "react-navigation-stack": "^1.10.3",
    "rest": "^2.0.0"
  },
  "devDependencies": {
    "@babel/core": "^7.7.7",
    "@babel/runtime": "^7.7.7",
    "@react-native-community/eslint-config": "^0.0.6",
    "@react-native-community/eslint-plugin": "^1.0.0",
    "babel-jest": "^24.9.0",
    "eslint": "^6.8.0",
    "eslint-config-prettier": "^6.9.0",
    "eslint-plugin-prettier": "^3.1.2",
    "jest": "^24.9.0",
    "license-checker": "^25.0.1",
    "metro-react-native-babel-preset": "^0.56.4",
    "reactotron-react-native": "^4.0.2"
  },
  "jest": {
    "preset": "react-native"
  },
}

I have seen:

but none of the solutions there worked for me.

@slhck slhck added the Bug label Jan 8, 2020
@react-native-bot react-native-bot added Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used. Platform: Linux Building on Linux. labels Jan 8, 2020
@nonotest
Copy link

I am also having this issue.

If you troubleshoot a bit, you will find out that there is an issue when building the haste-map.
Basically nothing under node_modules is loaded.
It is somehow linked to watchman

Try to 1) locate this line in node_modules/jest-haste-map/build/index.js

const crawl =
      canUseWatchman && this._options.useWatchman
        ? _watchman.default
        : _node.default;
  1. change watchman.default to _node.default (basically tell it to use _node.default)

and restart the packager with the reset option

I am not sure why nothing is loaded when using watchman, I have symlinks and use wml and I think it is somehow because of this.

Using macos, watchman 4.9.0 and latest rn

@slhck
Copy link
Author

slhck commented Jan 10, 2020

I will try to debug this, thanks. Watchman 4.9.0 has been the latest version since 2017, so it's weird that this would just occur now.

In the meantime I could reproduce on macOS and Linux, but my colleague running the same project under Linux has no issues running it. I will compare his utility versions.

I could somehow get my project running by reverting to an older commit, removing node_modules/package.lock, running npm i, then upgrading to the latest Git revision, then running npm i again. But I am not sure if this is not just a random observation.

@slhck
Copy link
Author

slhck commented Jan 10, 2020

I can confirm that changing the following in node_modules/jest-haste-map/build/index.js is a workaround:

    const crawl =
      canUseWatchman && this._options.useWatchman
        ? _watchman.default
        : _node.default;

Change to:

    const crawl =
      canUseWatchman && this._options.useWatchman
        ? _node.default
        : _node.default;

@nonotest
Copy link

so weird right! glad to see it's not just me

@slhck
Copy link
Author

slhck commented Jan 10, 2020

Note: Instead of modifying the file, I've uninstalled watchman and everything seems to run for the time being.

@Oscar3000
Copy link

@slhck any update on this issue as I also use wml in Linux and now, watchman also causes the same error as soon as I run watch-del-all on the project. I tried uninstalling watchman and installed it again but when I build my project and run wml start to copy the local dependency and run the project, I get another error that the local dependency is not found in node_modules but when I run watch-del-all again, it's found but the problem with the node_modules/jest-haste-map/build/index.js starts again.

@nonotest do you have another way of fixing it because going into that file to change it isn't good.

@nonotest
Copy link

nonotest commented Feb 5, 2020 via email

@slhck
Copy link
Author

slhck commented Feb 5, 2020

Sorry, I haven't figured out any other solution. Just get rid of watchman and everything should be fine.

@stale
Copy link

stale bot commented May 6, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label May 6, 2020
@aharbavets
Copy link

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

It's not fixed

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label May 8, 2020
@xxxapp
Copy link

xxxapp commented Jun 9, 2020

基础包过于简单导至babel_runtime_helpers_interopRequireDefault没有引入

@MatiSera
Copy link

MatiSera commented Aug 19, 2020

I'm having the same issue on iOS

EDIT: On both OS, Android and iOS

@thatsatul
Copy link

Getting same error.

Yarn add @babel/runtime not working.

react-native start --reset-cache not working.

Uninstalling watchman not working.

Below command is also not working for me.
watchman watch-del-all && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/react-* && rm -rf $TMPDIR/metro-* && rm -rf node_modules/ && yarn cache clean --force && yarn install && react-native start --reset-cache

React Native Environment Info:
System:
OS: macOS 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
Memory: 82.13 MB / 8.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.16.3 - /usr/local/bin/node
Yarn: 1.19.1 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 13.1, DriverKit 19.0, macOS 10.15, tvOS 13.0, watchOS 6.0
Android SDK:
API Levels: 28, 29
Build Tools: 28.0.3, 29.0.2
System Images: android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom, android-29 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5900203
Xcode: 11.1/11A1027 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.6 => 16.8.6
react-native: 0.59.9 => 0.59.9
npmGlobalPackages:
react-native-cli: 2.0.1

If there are some other solutions, please suggest. Totally stuck at this problem for 2 days.

@OlegPanfyorov
Copy link

Same situation, only watchman deleting helps but that's not the solution because my other project requires watchman

@GenericJam
Copy link

rm -rf /tmp/metro-* worked for me.

This is the last suggestion of the message to try these things:

  1. Clear watchman watches: watchman watch-del-all
  2. Delete node_modules: rm -rf node_modules and run yarn install
  3. Reset Metro's cache: yarn start --reset-cache
  4. Remove the cache: rm -rf /tmp/metro-*

@KEMBL
Copy link

KEMBL commented Oct 23, 2020

maybe it helps to fix the problem:
As far as I see from a Process Monitor the node.exe process tries to open interopRequireDefault file without .js extension
from node_modules\@babel\runtime\helpers\interopRequireDefault

and the operating system says PATH NOT FOUND

At the same time, interopRequireDefault.js lays in the same helpers folder.
As an experiment, I made a copy of interopRequireDefault.js without extension, after that file interopRequireDefault can be found by node process, but, even after receiving the success code from the operating system, the node process continues to search that file in parent folders as it usually does when cannot find some module.


my env: Windows 10, RN 0.63.3, Monorepo
command: npx react-native run-android --variant release
gives
Error: Unable to resolve module @babel/runtime/helpers/interopRequireDefault from app.tsx: @babel/runtime/helpers/interopRequireDefault could not be found within the project.

@KEMBL
Copy link

KEMBL commented Oct 24, 2020

as was mentioned above, looks like the presence of this property
watchFolders: [${__dirname}/../..],
in metro.config.js solves the problem. I have a monorepo and mobile app is one of packages, that is why I need ../..

BTW in my case, interopRequireDefault problem appears only when I make a release build with

react-native run-android --variant release

developing mode ( react-native run-android ) works fine even without metro.config.js file
Also, there is no problem if a project is not configured as Monorepo


npmPackages:
@react-native-community/cli: Not Found
react: 16.14.0 => 16.14.0
react-native: 0.63.3 => 0.63.3
react-native-windows: Not Found
npmGlobalPackages:
react-native: Not Found

@souli2020
Copy link

souli2020 commented Jul 11, 2022

I had the same error message but only when using Formik (in expo snack only ) so i found this solution:
added the formik dependecy to the json file

{
  "dependencies": {
    "yup": "*",
    **"formik": ">= 0.11.x < 2",**
    "expo-constants": "~13.1.1",
    "expo-image-picker": "~13.1.1",
    "@expo/vector-icons": "^13.0.0",
    "react-native-paper": "4.9.2",
    "react-native-gesture-handler": "~2.2.1",
    "react-native-gesture-handler/Swipeable": "~2.2.1",
    "babel-preset-react-native": "5.0.2"
  }
}

@Manil-SoftNEP
Copy link

I accidentally deleted my node_modules and package-lock.json. I used npm i to regenerate them and suddenly I was getting the above error. I solved it by uninstalling the app and using the following commands.

npx jest --clearCache

npm update

npm i

npx react-native run-android

Hope this helps you.

@iegik
Copy link

iegik commented Aug 30, 2022

Resolved by npm install next
Explanation: npx next dev will run either from global location (~/.npm/_npx/.../node_modules/next/dist/client) or current directory (when next is installed into ./node_modules).
Because you probably don't have next installed, it searches in global location.

@tiagotedsky
Copy link

For anyone coming with the same issue but on iOS make sure you don't have your phone plugged in.

@nandorojo
Copy link

For anyone seeing this with Expo in a monorepo, I had the wrong workspaceRoot here:

/**
 * @type {import('expo/metro-config')}
 */
const { getDefaultConfig } = require('expo/metro-config')
const path = require('path')

const projectRoot = __dirname
const workspaceRoot = path.resolve(__dirname, '../..')

const config = getDefaultConfig(projectRoot)

config.watchFolders = [workspaceRoot]
config.resolver.nodeModulesPaths = [
  path.resolve(projectRoot, 'node_modules'),
  path.resolve(workspaceRoot, 'node_modules'),
]

config.resolver.extraNodeModules = {
  modules: workspaceRoot,
}

console.log('config', config)

module.exports = config

I had to change it from .. to ../...

@sw0ok
Copy link

sw0ok commented Jun 18, 2023

For anyone seeing this with Expo in a monorepo, I had the wrong workspaceRoot here:

/**
 * @type {import('expo/metro-config')}
 */
const { getDefaultConfig } = require('expo/metro-config')
const path = require('path')

const projectRoot = __dirname
const workspaceRoot = path.resolve(__dirname, '../..')

const config = getDefaultConfig(projectRoot)

config.watchFolders = [workspaceRoot]
config.resolver.nodeModulesPaths = [
  path.resolve(projectRoot, 'node_modules'),
  path.resolve(workspaceRoot, 'node_modules'),
]

config.resolver.extraNodeModules = {
  modules: workspaceRoot,
}

console.log('config', config)

module.exports = config

I had to change it from .. to ../...

I am setting a monorepo right now and your pointer was a solution for me, editing metro.config.js from

module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
};

to

const resolve = require('path').resolve;
const WORKSPACE_ROOT = resolve(PROJECT_ROOT, '../../');

console.log(PROJECT_ROOT);
module.exports = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: true,
      },
    }),
  },
  watchFolders: [WORKSPACE_ROOT],
};

Leglaw added a commit to Leglaw/template-app-react-native-todo that referenced this issue Oct 16, 2023
Update to metro.config.js explained in this issue:
facebook/react-native#27712
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Dec 16, 2023
Copy link

This issue was closed because it has been stalled for 7 days with no activity.

@shirakaba
Copy link

Of interest: I found this happens with react-native start yet not expo start (though this is in a custom React Native Windows with Expo project, so may not apply to others).

As mentioned above, in my case the solution was to add the monorepo root to the watch folders: #27712 (comment)

@sminrana
Copy link

sminrana commented Nov 17, 2024

Deleting watchman fixes it.

@shirakaba
Copy link

shirakaba commented Dec 12, 2024

Understood a bit better now. This happens when running bare React Native projects in monorepos because the bare React Native's default Metro config does not include the monorepo root as a watchFolder (and watchFolders is more that just folders to watch for changes in – it's where Metro looks for your node_modules). The Expo Metro config, by contrast, does support monorepos.

What you can do is use Microsoft's @rnx-kit/metro-config instead, which supports monorepos out of the box (as well as supporting react-native-macos and react-native-windows):

npm install --save-dev @rnx-kit/metro-config

Now change your metro.config.js as follows, just following the default setup from their docs:

-const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
+const { makeMetroConfig } = require("@rnx-kit/metro-config");
-
-/**
- * Metro configuration
- * https://facebook.github.io/metro/docs/configuration
- *
- * @type {import('metro-config').MetroConfig}
- */
-const config = {};
-
-module.exports = mergeConfig(getDefaultConfig(__dirname), config);
+module.exports = makeMetroConfig({
+  transformer: {
+    getTransformOptions: async () => ({
+      transform: {
+        experimentalImportSupport: false,
+        inlineRequires: false,
+      },
+    }),
+  },
+});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Platform: Linux Building on Linux. Stale There has been a lack of activity on this issue and it may be closed soon. Tech: Bundler 📦 This issue is related to the bundler (Metro, Haul, etc) used.
Projects
None yet
Development

No branches or pull requests