facebook/react-native

Cannot find module 'warnOnce' from 'react-native-implementation.js'

Closed this issue Β· 77 comments

πŸ› Bug Report

When requiring react-native in node context (e.g. jest) module warnOnce cannot be found.
● Test suite failed to run

Cannot find module 'warnOnce' from 'react-native-implementation.js'

To Reproduce

TestFile.js

import { PixelRatio } from 'react-native'

export default class TestFile {
}

TestFile.test.js

import TestFile from './TestFile'

test('test', () => {
  ...
})

Expected Behavior

warnOnce should be found and loaded just like invariant.

Workaround

As a workaround I'm mocking react-native dependency with jest.doMock('react-native', () => reactNativeMock))

Environment

info
  React Native Environment Info:
    System:
      OS: macOS 10.14.3
      CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
      Memory: 3.06 GB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 8.10.0 - ~/.nvm/versions/node/v8.10.0/bin/node
      Yarn: 1.13.0 - ~/.nvm/versions/node/v8.10.0/bin/yarn
      npm: 6.8.0 - ~/.nvm/versions/node/v8.10.0/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 25, 26, 27, 28
        Build Tools: 21.1.2, 23.0.1, 25.0.0, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.3
        System Images: android-19 | Google APIs Intel x86 Atom, android-25 | Google APIs Intel x86 Atom_64, android-27 | Google APIs Intel x86 Atom
    IDEs:
      Android Studio: 3.3 AI-182.5107.16.33.5199772
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3
      react-native: 0.59.1 => 0.59.0
    npmGlobalPackages:
      create-react-native-app: 2.0.2
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7
      react-native-rename: 2.4.0
c4ss commented

Getting the same error when trying to run yarn run metro build index.js --out ./dist/

with versions

    react 16.6.3
    react-native 0.58.6

Stemming from #22109 so maybe babel issue?

I have this same issue when running our react-native ios app.
with versions
"react": "^16.8.5", "react-native": "^0.59.2",

"Cannot find module 'warnOnce' "error, is there any solution for that.

also i dont use pod *

React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
Binaries:
Node: 10.15.3 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
Android SDK:
API Levels: 23, 25, 26, 27, 28
Build Tools: 23.0.1, 24.0.2, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.1, 28.0.3
System Images: android-28 | Google APIs Intel x86 Atom_64
IDEs:
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.3 => 16.8.3
react-native: 0.59.2 => 0.59.2
npmGlobalPackages:
react-native-cli: 2.0.1

my xcode error::

warning: the transform cache was reset.
Loading dependency graph, done.
error index.js: Cannot find module 'warnOnce'

  • [[ false != true ]]
  • [[ ! -f /Users/me/Library/Developer/Xcode/DerivedData/build/Build/Products/Release-
    iphonesimulator/myapp.app/main.jsbundle ]]
  • echo 'error: File /Users/me/Library/Developer/Xcode/DerivedData/build/Build/Products/Release-
    iphonesimulator/myapp.app/main.jsbundle does not exist. This must be a bug with'
    error: File /Users/me/Library/Developer/Xcode/DerivedData/build/Build/Products/Release-
    iphonesimulator/myapp.app/main.jsbundle does not exist. This must be a bug with

@flodev were u able to solve that issue??? or pass ?? thx

It is still an issue, have you found a solution?

This is what I have tried:

  1. remove all references of warnOnce in react-native-implementation.js
  • run the iOS build again
  • got the following error:
    bundling failed: Error: Cannot find module 'warnOnce' at Function.Module._resolveFilename (module.js:547:15) at Function.Module._load (module.js:474:25) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object.<anonymous> (/Users/gzcheng/Documents/ellentube-mobile/node_modules/react-native/Libraries/react-native/react-native-implementation.js:14:18) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12)
  1. copy the warnOnce.js from /node_modules⁩/react-native/⁨Libraries⁩/Utilities to /node_modules⁩/react-native⁩/⁨Libraries⁩/⁨react-native⁩.
  • got a duplicate modul name error.

That tells me that packager is able to find the warnOnce module. I am not an expert on this, but it sounds like some we maybe using some incompatible library versions?
This issue has been bothering for about a week... would like to have it resolved soon...

@rickhanlonii , i tried your suggestion from other issue page, i still see the error, i know limited people and so many new xcode ticket :( , good luck for all tickets

Hi @GreenRidingHood,
I haven't had the issue while building and deploying to devices. It was only an issue when I tried to include the react-native dependency in a jest test.
In my first post I described how I bypassed the issue with mocking the react-native dependency.
My guess is that you have included a script somewhere in you build process that tries to access react-native dependency. Have you tried setting up a new project and deploying? Maybe you can compare your project setup with the new project setup and figure it out.

@flodev i will try your suggestions, :) about script , anything can happen. While i was trying to run new xcode 10 , so many errors, issues or fixes i go over, i even dont remember :) thx

Have someone found a fix yet? I am stuck on this

I'm facing this issue for the past 2 days. Tried many solutions but no result yet
I don't think so downgrading the versions of libraries will be a permanent solution for this, should find some valid solutions.

What is the appropriate version to downgrade to, then?

I created a new react-native project with a newer version (0.59.4). still facing the same issue
I downgraded the react-native version to 0.58.0 and created the web-pack configuration.all worked fine. I think the actual issue is with the warnOnce.js file. @amunim can u try with the older version(0.58.0)

@sonukj yeah.. i downgraded to 0.58.0 , i dont have that error anymore, but glog , i wonder when i will be able to use xcode10 and react-native without any problem :(

@sonukj yeah.. i downgraded to 0.58.0 , i dont have that error anymore, but glog , i wonder when i will be able to use xcode10 and react-native without any problem :(

did you try updating your pod???

facing same issue .
I have used below version.

  "react": "16.8.3",
   "react-native": "^0.59.4",

any workaround?

Having the same issue with "react-native": "^0.59.4"

Update:
I finally gave up on react native and from this docs facebook I used expo cli to generate my app Which does not use flow so even if my IDE(VS Code) has flow enabled the project doesn't support it hence it works flawlessly.

For those Who want to use native features such as AndroidManifest, opening up the project with android studio and/or XCode simply run npm run efect or yarn eject for yarn

This is the only solution(s) I've found and I don't mind using it.

I have resolved this issue, and for me the problem was with .bablerc.
For some reason I had "presets": ["module:react-native", "react-native"]
instead of "presets": ["module:metro-react-native-babel-preset"]

But from Babel 7 onwards they are not using .babelrc config instead having separate babel.config.js file. Can you tell me reason how the problem solved by changing the configuration?

@sonukj to make this work in babel.config.json just paste the correct line which alex has mentioned AS IT IS and apply the comma's to fix the json if a compile error occurs
If it already consists a presets object then append ["module:metro-react-native-babel-preset"] to the value of it

@sonukj I might be wrong, but as I understand from here and here, .babelrc is applied to the single package only, when babel.config.json is a general project-wide configuration. In my case, I am using .babelrc because of babel-plugin-module-resolver

@sonukj @amunim solution did not work for me,
i have babel.config.json file and like this,

module.exports = {
presets: ['module:metro-react-native-babel-preset'],
};

but still having that error in my xcode

  • node /Users/me/dev/myapp/node_modules/react-native/cli.js bundle --entry-file index.js --platform ios --dev false --reset-cache --bundle-output /Users/me/dev/myapp/ios/build/Build/Products/Release-iphonesimulator/myapp.app/main.jsbundle --assets-dest /Users/me/dev/myapp/ios/build/Build/Products/Release-iphonesimulator/myapp.app
    warning: the transform cache was reset.
    Loading dependency graph, done.
    error index.js: Cannot find module 'warnOnce'. Run CLI with --verbose flag for more details.
  • [[ false != true ]]
  • [[ ! -f /Users/me/dev/myapp/ios/build/Build/Products/Release-iphonesimulator/myapp.app/main.jsbundle ]]
  • echo 'error: File /Users/me/dev/myapp/ios/build/Build/Products/Release-iphonesimulator/myapp.app/main.jsbundle does not exist. This must be a bug with'
    error: File /Users/me/dev/myapp/ios/build/Build/Products/Release-iphonesimulator/myapp.app/main.jsbundle does not exist. This must be a bug with

react-native version 0.59.5 still have same issue,
if i downgraded to 58, yes i dont get this error anymore, but i am getting this kind of error

::ffff:127.0.0.1 - - [24/Apr/2019:19:43:46 +0000] "GET /index.ios.bundle?platform=ios&dev=true&minify=false HTTP/1.1" 200 - "-" "myapp/6 CFNetwork/975.0.3 Darwin/17.7.0"
Error: Unable to resolve module ./index.ios from /Users/me/dev/myapp/.: The module ./index.ios could not be found from /Users/me/dev/myapp/.. Indeed, none of these files exist:

  • /Users/me/dev/myapp/index.ios(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
  • /Users/me/dev/myapp/index.ios/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx)
    at ModuleResolver.resolveDependency (/Users/me/dev/myapp/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:163:15)
    at ResolutionRequest.resolveDependency (/Users/me/dev/myapp/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:52:18)
    at DependencyGraph.resolveDependency (/Users/me/dev/myapp/node_modules/metro/src/node-haste/DependencyGraph.js:273:16)
    at /Users/me/dev/myapp/node_modules/metro/src/lib/transformHelpers.js:261:42
    at Server. (/Users/me/dev/myapp/node_modules/metro/src/Server.js:935:41)
    at Generator.next ()
    at asyncGeneratorStep (/Users/me/dev/myapp/node_modules/metro/src/Server.js:73:24)
    at _next (/Users/me/dev/myapp/node_modules/metro/src/Server.js:93:9)
    at process._tickCallback (internal/process/next_tick.js:68:7)

anyway after that RN58 error, i am using 59 again but ..
... agghh i have to use xcode 10 with react-native :(

xcode Version 10.1 (10B61)
react-native-cli: 2.0.1
react-native: 0.59.5

Okay, this is somewhat magical, but for me the problem was fixed when I deleted my jest.config.js and moved config to package.json, even despite the contents being identical.

Okay, this is somewhat magical, but for me the problem was fixed when I deleted my jest.config.js and moved config to package.json, even despite the contents being identical.

Didn't work for me... I don't know why

The workaround to mock it is not working for me.

Currently investigating the issue. Here's what I have so far in case anyone wants to jump in with me. We might be able to do a pull request to resolve this issue.

File: node_modules/react-native/Libraries/react-native/react-native-implementation.js
I replaced

const warnOnce = require('warnOnce');

with

const warnOnce = require('../Utilities/warnOnce');

Then in node_modules/react-native/Libraries/Utilities/warnOnce.js I commented out all the flow annotations (as node cannot use a Flow-annotated file directly).

That fixed it. Error gone.

However, running yarn jest -u now gives me:

Cannot find module 'StyleSheet' from 'react-native-implementation.js'

    However, Jest was able to find:
    	'../Card.js'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

StyleSheet is required in line 302 of node_modules/react-native/Libraries/react-native/react-native-implementation.js, but is defined separately with Flow annotations in node_modules/react-native/Libraries/StyleSheet/StyleSheet.js:

  get StyleSheet() {
    return require('StyleSheet');
  }

My guess is that if I fix up this file by removing the type annotations and making the require path explicit in terms of putting down the relative paths instead, this error will disappear and the next React Native element that my component uses will be declared missing.

As an indirect test, I removed the use of StyleSheet in my React Native component and lo and behold, the error disappeared. Now yarn jest -u gives me this:

  The Card Component
    βœ• should render with children correctly (17ms)
    βœ• should render with styles and children correctly (1ms)

  ● The Card Component β€Ί should render with children correctly

    Cannot find module 'View' from 'react-native-implementation.js'

      15 |     const component = (
      16 |       <Card>
    > 17 |         <View />
         |          ^
      18 |       </Card>
      19 |     )
      20 |     const tree = renderer.create(component).toJSON()

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.get View [as View] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:170:12)
      at Object.<anonymous> (src/__tests__/Card.test.js:17:10)

  ● The Card Component β€Ί should render with styles and children correctly

    Cannot find module 'View' from 'react-native-implementation.js'

      29 |         }}
      30 |       >
    > 31 |         <View />
         |          ^
      32 |       </Card>
      33 |     )
      34 |     const tree = renderer.create(component).toJSON()

      at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:229:17)
      at Object.get View [as View] (node_modules/react-native/Libraries/react-native/react-native-implementation.js:170:12)
      at Object.<anonymous> (src/__tests__/Card.test.js:31:10)

From all of this, I gather that React Native is failing to import its own files that are defined in node_modules/react-native/Libraries. It cannot directly import them because those files are Flow-annotated, so there must be a build step somewhere that compiles the code and places the result inside node_modules/react-native/node_modules so that react-native-implementation.js can treat them like modules in the way it's currently treating them.

However, ls node_modules/react-native/node_modules gives me:

ansi-regex		debug			is-fullwidth-code-point	path-type		read-pkg-up		ws
chardet			external-editor		load-json-file		pretty-format		strip-ansi		yargs
cliui			inquirer		ms			read-pkg		strip-bom		yargs-parser

Which means there is some disconnect between how these library files are being declared and how they're being consumed. The in-between step is missing.

I've made a repository that reproduces the issue if anyone wants to take a gander. I'm not an expert on how RN works internally so help is definitely needed. It seems from reading the (now closed) duplicate issue #24065 that a haste map isn't being properly created, but I'm not sure if the issue lies with jest or RN (but seeing as how the same issue pops up in XCode, probably with RN?)

https://github.com/iamrenejr/jest-rn-repro

Just confirmed on the repo above that downgrading RN to 0.58.6 or lower will make the issue go away, so the solution is somewhere there. It only appears exactly on version 0.59.0 and up. However, we want to use hooks so that's not a good option for us.

After several hours of this, this tool helped me out by showing what changes happened between the two versions, and just copying the changes that might be the reason why 0.59 works for some but not others.

So this is the thing that fixed it for me. Others with the issue please confirm if this works for you:

In package.json

"jest": {
    "preset": "react-native"
}

I'm not sure if that works in XCode since this solution is jest-specific but it means there's some babel magic going on under the hood that you should be adding to your XCode setups to get this to work. Hopefully this helps though.

Nice research. I guess I should have listed my jest config, I assumed that everyone already has β€œpreset”: β€œreact-native”.

Important thing to note here that at least for me it only worked when inside of package.json, not jest.config.js

Can anyone else confirm that what @iamrenejr wrote is the correct approach to solve this? It seems to me too that it's a matter of babel 7 & jest have a complex dynamic.

Which is also I'd recommend doing what written here #23943 (comment)

@kelset @undsoft I dont have jest.config.js file
my package.json file is:

{
  "name": "myapp",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "lint": "eslint .",
    "android": "react-native run-android",
    "ios": "react-native run-ios"
  },
  "dependencies": {
    "and": "^0.0.3",
    "react": "^16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "^0.59.5",
    "react-native-camera": "^2.1.1",
    "react-native-cookies": "^3.3.0",
    "react-native-fabric": "^0.5.2",
    "react-native-html-to-pdf": "^0.7.0",
    "react-native-idle-timer": "^2.1.5",
    "react-native-image-to-base64": "^0.1.0",
    "react-native-keychain": "^3.1.1",
    "react-native-orientation": "^3.1.3",
    "react-native-pdf": "^5.0.12",
    "react-native-signature-capture": "^0.4.9",
    "react-native-splash-screen": "^3.2.0",
    "react-native-vector-icons": "^6.4.2",
    "react-native-version-number": "^0.3.5",
    "rm": "^0.1.8",
    "rn-fetch-blob": "^0.10.15"
  },
  "devDependencies": {
    "@babel/core": "^7.4.4",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-proposal-function-bind": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/preset-env": "^7.4.4",
    "@babel/preset-react": "^7.0.0",
    "@babel/runtime": "^7.4.3",
    "@react-native-community/eslint-config": "^0.0.3",
    "babel-jest": "^24.7.1",
    "babel-plugin-module-resolver": "^3.2.0",
    "babel-plugin-transform-function-bind": "^6.22.0",
    "babel-preset-react-native": "^4.0.1",
    "jest": "^24.7.1",
    "metro": "^0.53.1",
    "metro-core": "^0.53.1",
    "metro-react-native-babel-preset": "^0.53.1",
    "react-test-renderer": "16.8.3"
  },
  "rnpm": {
    "assets": [
      "assets/fonts/Nevis",
      "assets/fonts/OpenSans",
      "assets/fonts/Nunito",
      "assets/fonts/Roboto",
      "assets/fonts/fontello.ttf"
    ]
  },
  "jest": {
    "preset": "react-native"
  }
}

my babel.config.js file is:

  presets: ['module:metro-react-native-babel-preset'],
};

my metro.config.js file is:

/**
 * Metro configuration for React Native
 * https://github.com/facebook/react-native
 *
 * @format
 */

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

my rn-cli.config.js file is:

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  resolver:{
    blacklistRE: blacklist([
      /nodejs-assets\/.*/,
      /android\/.*/,
      /ios\/.*/
    ])
  },
};

MacOS Version 10.14.4
npm 6.4.1
react-native-cli: 2.0.1
react-native: 0.59.5
node v10.15.3
watchman 4.9.0

**i dont use cocoapods
**actually i am not using/doing any testing(maybe i should just delete all jest things in the package.json)
**yes , if i downgrade react-native 58.0.0 i dont get any warnOnce error, but different issues coming up

@GreenRidingHood Since your issue is with XCode, I don't think setting up jest configs is going to fix the issue for you. Have you tried doing this?

I think since you're running into an issue no matter what version, even in 0.58.0, there's probably something else going on that's causing these errors for you. Have you tried setting up a new project from scratch as @flodev suggested?

@iamrenejr i did not have any 3rd party error when i downgraded, i mean i had probably but fixed with your suggested link before or other 3rd party error solutions from :) i dont even remember how many error fixed for that xcode 10 . thanks by the way. i created new react-native project and worked(i mean at least i saw welcome react-native) but after i copy my stuff include package json i get some errors, maybe i should try again and see what was error i couldnt passed or i shouldnt copy my package json
and add needed stuff one at a time if i get errors.

Having the same issue here, I am on RN 0.59.8

For users encountering this issue, does this fix help?

  • Open node_modules/react-native/jest/setup.js
  • Add the following line: (doesn't really matter where - the end of the file, or near the other calls to .mock())
jest.mock('warnOnce', () => () => {});

If it works, I can submit this as a pull request and preferably try to get this in by the next release of RN.

cc @jogboms

@empyrical it didn't work for me, returns same errors

@hasan-li Are you also using macOS like the other users having the problem? I am suspecting this may be a Mac-only problem (as I'm not encountering this bug on Ubuntu)

@empyrical yes, it was macOS

Okay, this is somewhat magical, but for me the problem was fixed when I deleted my jest.config.js and moved config to package.json, even despite the contents being identical.

Same for me. This is weird and should be addressed so we understand it better

Okay, this is somewhat magical, but for me the problem was fixed when I deleted my jest.config.js and moved config to package.json, even despite the contents being identical.

Same for me. This is weird and should be addressed so we understand it better

In my case it was because I didn't have babel-preset-react-native on my dev dependencies.

npm i --save-dev babel-preset-react-native

Make sure you don't have multiple configs for jest. If you have jest.config.js in your root and then "jest": { "preset": "react-native" } in package.json then you'll be thrown this error. Move the preset setting into jest.config.js and remove the entry in package.json

Also, please don't use @tinmar33's suggestion for the babel preset. Stick to metro-react-native-babel-preset

In my case,
-> I have updated my existing react-native version from "0.55.3" to "0.59.8".
-> I am able to build and run the app successfully.
-> Where as while running "npm test" i face the below issue

  1. With "preset" in package.json
    "jest": {
    "preset": "react-native"
    }
    error message: Validation Error: Preset react-native not found.

  2. Without "presset" in package.json
    error message : "Cannot find module 'warnOnce' from 'react-native-implementation.js'"

Is there any additional configuration need to be changed for "npm test" ?

This was fixed on master (we stopped using haste) and it will be part of 0.61. If you are using a previous version of RN, make sure you are using "jest": { "preset": "react-native" } in your setup properly, otherwise it will not work.

@cpojer May I know when the 0.61 is going to out? since have to get the fix for getting our unit tests passed and role out to the market before Aug 01 due to the Google play store 64-bit compliance. Thanks, Much appreciated.

@cpojer

"jest": { "preset": "react-native" } is set in package.json but still being met with this error.

react-native 0.60.4 still having this issue after trying all above :(
react-native 0.60.* - all versions have warnOnce issue.

UPD: I'm using macOS, don't know if this makes any difference πŸ€·β€β™‚οΈ

I have resolved this issue, and for me the problem was with .bablerc.
For some reason I had "presets": ["module:react-native", "react-native"]
instead of "presets": ["module:metro-react-native-babel-preset"]

Awesome. That worked. <3

  • OS: macOS High Sierra 10.13.6
  • react: 16.8.6
  • react-native: 0.60.4

Still have this issue with the above steps:

  • OS: macOS Mojave 10.14.6
  • react: 16.8.6
  • react-native: 0.60.4
  • Jest: 24.8.0

I'm hoping RN v0.61 will come out soon.

None of the above solutions worked for me either.

I ended up creating a brand new project on RN 0.60.4, which built/ran without error. Then ported over all my app code to the new project, made sure that built/ran fine, then carefully copied the relevant iOS files back over to my original project. It was a pain, and unfortunately I don't know which file fixed it, but now it runs without error on RN 0.60.4.

Might be worth a shot if you're still stuck on this.

@cpojer Is there any way for us to work around this until .61 is released? I'm using your jest preset suggestion, but am still getting the error.

@IAmMarcellus could you post the contents of your project's .babelrc or babel.config.js? (and - if both files are present, include that information too)

@empyrical my babel.config.js:

module.exports = { presets: ['module:metro-react-native-babel-preset'], };

And I'm not using a .babelrc

@empyrical If I open node_modules/react-native/Libraries/Utilities/warnOnce.js, my IDE show an error in places saying that "types can only be used in a .ts file". Could that have anything to do with the problem?

I realize by adding module.exports = { presets: ['module:metro-react-native-babel-preset'], }; I can run some command like "babel src/ -d lib/ " without "warnOnce" error. But I have the error if I try to run this command "yarn run metro build index.js -O index.android.bundle" or "yarn android"

I'm having this same issue when trying to run tests, but instead of jest I'm using mocha (planning on migrating to jest in the future).

@empyrical Any suggestions? I had to update to 0.60.X to implement the facebook SDK, but I can't do anything now that I've updated because of this issue. I've followed all of the suggestions, but nothing is working and I really need to release the update for my app, but can't until this is resolved so any help would be MUCH appreciated.

I was fixed this error. I add module.exports = { preset: 'react-native', ....} in file jest.config.js.
I used "react": "16.8.6", "react-native": "0.60.5",
Device macOS Mojave version 10.14.5

I have no idea why, but for me this problem was solved when I removed this roots config:
roots: [ '<rootDir>/src' ],
from jest.config.js
Out of the box by react-native-cli I had preset: 'react-native'.

If you have jest.config.js file inside your root directory,
you should add preset: 'react-native', property to module.export

so It will look like:

jest.config.js

module.exports = {
	preset: 'react-native',
        ...
};

I'm having the same issue, Cannot find module 'Dimensions'. No mocks for react-native are working. On 0.60.6 everything works fine but both 0.61.0 and 0.61.1 are giving this error.

Why is this issue closed? Still facing the same issue, on different computers.

info 
  React Native Environment Info:
    System:
      OS: Linux 4.15 Ubuntu 18.04.3 LTS (Bionic Beaver)
      CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
      Memory: 482.58 MB / 11.63 GB
      Shell: 5.4.2 - /bin/zsh
    Binaries:
      Node: 8.10.0 - /usr/bin/node
      Yarn: 1.17.3 - /usr/bin/yarn
      npm: 3.5.2 - /usr/bin/npm
    npmPackages:
      react: 16.8.3 => 16.8.3 
      react-native: 0.59.8 => 0.59.8 
    npmGlobalPackages:
      react-native-cli: 2.0.1

Same issue. Any solution ?

I'm getting same issue for a bunch of things after upgrading to 0.61.0 (from 0.59.10) including Alert, Dimensions, Platform.

To workaround it I've started mocking react-native itself and then mock whichever named export I need...

// From
jest.mock('Platform', () => ({
    OS: 'someOS'
}));

// To
jest.mock('react-native', () => ({
  Platform: {
    OS: 'someOS'
  }
}));

Still think there's an issue here though as I'm able to reproduce on a fresh React Native project.

I'm confused. 0.61 is supposed to not use haste anymore, right? This issue is caused by haste. Can you guys give a new sample repro (the one I provided before was for a pre-0.60 version)?

I was using version 0.60.0 and this bug as occurring.
Upgraded to the latest version as of now, which is 0.61.2 and things went smoothly.

I have The Same Issue when i downgrade from 0.61 into 0.60

FI : I came across this bug today, and it was because, I was importing a file that imported himeself React while not being in a react environment (but babel-node for a script execution).

In case someone's still facing this issue using RN < 0.60 (I'm using 0.59.10):

  • Be sure to remove the "preset": "react-native" from your package.json;
  • Create your jest.config.json and be sure to include "preset": "react-native" inside it. In my case I was trying to setup Enzyme and the enzyme-to-json serializer:
{
  "preset": "react-native",
  "setupFiles": [
    "<rootDir>/src/tests/setupTests.js"
  ],
  "snapshotSerializers": [
    "enzyme-to-json/serializer"
  ]
}
  • Inside setupTests.js I wrote the adapter configuration as shown in the Enzyme website:
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'

Enzyme.configure({ adapter: new Adapter() })

You should now be able to test your components

I'm using react-native (0.61) in a yarn workspaces monorepo.

My fix was to makes sure that jest maps all react-native imports to the version of react-native in our app (and not one from the monorepo root).

This can be done by adding a moduleNameMapper rule in jest.config.js:

module.exports = {
  preset: "react-native",
  moduleNameMapper: {
    "^react-native$": require.resolve("react-native")
  }
}

moduleNameMapper | jest docs

If you have jest.config.js file inside your root directory,
you should add preset: 'react-native', property to module.export

so It will look like:

jest.config.js

module.exports = {
	preset: 'react-native',
        ...
};

Thanks @alexander-mozolevsky, this work for me :)

Changing metro.config.js to this helped me

const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
  resolver: {
    blacklistRE: blacklist([
      /node_modules\/.*\/node_modules\/react-native\/.*/,
    ])
  },
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
};

If you're working with a monorepo and are using components from a common package, try adding this to the package.json of the common package:

"devDependencies": {
    ...
},
"private": true,
"workspaces": {
	"nohoist": [
		"react-native",
		"react-native/**",
		"react",
		"react/**"
	]
}

Then get rid of all node_modules and install everything again.

Hello try downgrade nodejs like i did, now im using node 10.20.1 and working good πŸ‘

Nothing worked :(
For me it's associated with a particular package, @shoutem/ui. I'll cross-post an issue there.

same here