From dd63cbf5df4fad4fc355c885f931478be636f8e6 Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Tue, 3 Oct 2017 05:42:21 -0700 Subject: [PATCH] Add a check in InitializeCore.js that PlatformConstants native module is present Summary: In some enviroments PlatformConstants native module may not be presented in a project, which results in a call to undefined property and a RedBox Reviewed By: javache Differential Revision: D5960879 fbshipit-source-id: 80aecbe2f2a61cb410abd5f0dce8ba855e166991 --- Libraries/Core/InitializeCore.js | 33 +++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Libraries/Core/InitializeCore.js b/Libraries/Core/InitializeCore.js index 51edfeb1c9beac..4990723fa36aa1 100644 --- a/Libraries/Core/InitializeCore.js +++ b/Libraries/Core/InitializeCore.js @@ -116,21 +116,24 @@ if (!global.__fbDisableExceptionsManager) { ErrorUtils.setGlobalHandler(handleError); } -const formatVersion = version => - `${version.major}.${version.minor}.${version.patch}` + - (version.prerelease !== null ? `-${version.prerelease}` : ''); - -const ReactNativeVersion = require('ReactNativeVersion'); -const nativeVersion = require('NativeModules').PlatformConstants.reactNativeVersion; -if (ReactNativeVersion.version.major !== nativeVersion.major || - ReactNativeVersion.version.minor !== nativeVersion.minor) { - throw new Error( - `React Native version mismatch.\n\nJavaScript version: ${formatVersion(ReactNativeVersion.version)}\n` + - `Native version: ${formatVersion(nativeVersion)}\n\n` + - 'Make sure that you have rebuilt the native code. If the problem persists ' + - 'try clearing the watchman and packager caches with `watchman watch-del-all ' + - '&& react-native start --reset-cache`.' - ); +const {PlatformConstants} = require('NativeModules'); +if (PlatformConstants) { + const formatVersion = version => + `${version.major}.${version.minor}.${version.patch}` + + (version.prerelease !== null ? `-${version.prerelease}` : ''); + + const ReactNativeVersion = require('ReactNativeVersion'); + const nativeVersion = PlatformConstants.reactNativeVersion; + if (ReactNativeVersion.version.major !== nativeVersion.major || + ReactNativeVersion.version.minor !== nativeVersion.minor) { + throw new Error( + `React Native version mismatch.\n\nJavaScript version: ${formatVersion(ReactNativeVersion.version)}\n` + + `Native version: ${formatVersion(nativeVersion)}\n\n` + + 'Make sure that you have rebuilt the native code. If the problem persists ' + + 'try clearing the watchman and packager caches with `watchman watch-del-all ' + + '&& react-native start --reset-cache`.' + ); + } } // Set up collections