react-native: Cannot find module 'warnOnce' from 'react-native-implementation.js'
š 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
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 37
- Comments: 77 (6 by maintainers)
Commits related to this issue
- Fix Settings tests failing See https://github.com/facebook/react-native/issues/23943 — committed to matei-radu/react-native-in-app-browser by matei-radu 5 years ago
- Introduce generic `warnOnce` function for warning messages (#22109) Summary: This pull request adds a function called `warnOnce` that prints a warning message to the console once per session. It use... — committed to facebook/react-native by empyrical 5 years ago
- fix(mobile): upgraded react native version affects: @natds/mobile Upgraded react native version due to bug on jest execution described here: https://github.com/facebook/react-native/issues/23943 — committed to natura-cosmeticos/natds-js by robertLichtnow 5 years ago
- fix(mobile): upgraded react native version affects: @natds/mobile Upgraded react native version due to bug on jest execution described here: https://github.com/facebook/react-native/issues/23943 — committed to natura-cosmeticos/natds-js by robertLichtnow 5 years ago
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"]
Have someone found a fix yet? I am stuck on this
For users encountering this issue, does this fix help?
node_modules/react-native/jest/setup.js
.mock()
)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
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 š¤·āāļø
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.Why is this issue closed? Still facing the same issue, on different computers.
@cpojer
"jest": { "preset": "react-native" }
is set inpackage.json
but still being met with this error.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" }
inpackage.json
then youāll be thrown this error. Move the preset setting intojest.config.js
and remove the entry inpackage.json
Also, please donāt use @tinmar33ās suggestion for the babel preset. Stick to
metro-react-native-babel-preset
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
@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
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.<anonymous> (/Users/me/dev/myapp/node_modules/metro/src/Server.js:935:41) at Generator.next (<anonymous>) 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
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
oryarn eject
for yarnThis is the only solution(s) Iāve found and I donāt mind using it.
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ā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ā¦Still think thereās an issue here though as Iām able to reproduce on a fresh React Native project.
@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.
If you have jest.config.js file inside your root directory, you should add
preset: 'react-native',
property tomodule.export
so It will look like:
jest.config.js
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.
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)
@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.
Still have this issue with the above steps:
Iām hoping RN v0.61 will come out soon.
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
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.
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 replacedwith
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: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 innode_modules/react-native/Libraries/StyleSheet/StyleSheet.js
: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. Nowyarn jest -u
gives me this: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 insidenode_modules/react-native/node_modules
so thatreact-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: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.
same here
Nothing worked š¦ For me itās associated with a particular package,
@shoutem/ui
. Iāll cross-post an issue there.Changing metro.config.js to this helped me
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.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.5Iā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 it didnāt work for me, returns same errors
Having the same issue here, I am on RN 0.59.8
This is what I have tried:
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)
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ā¦