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

Star imports from react-native cause "Native module cannot be null" error on startup #12018

Closed
dphilipson opened this issue Jan 22, 2017 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@dphilipson
Copy link

dphilipson commented Jan 22, 2017

Description

As the title says. If React Native is star-imported, e.g.

import * as RN from 'react-native';

then the app shows a "Native module cannot be null" redbox on startup and fails to load. Similarly, Jest tests fail with the same error.

Reproduction

I can reproduce this by taking the starting project from react-native init AwesomeProject and adding the following line to index.ios.js.

import * as RN from 'react-native';

The app then shows the following error on startup:

image

Likewise, npm test fails with the following error:

 FAIL  __tests__/index.ios.js
  ● Test suite failed to run

    Invariant Violation: Native module cannot be null.

      at invariant (node_modules/fbjs/lib/invariant.js:44:15)
      at Linking.NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:32:1)
      at new Linking (node_modules/react-native/Libraries/Linking/Linking.js:119:141)
      at Object.<anonymous> (node_modules/react-native/Libraries/Linking/Linking.js:191:16)
      at Object.get Linking [as Linking] (node_modules/react-native/Libraries/react-native/react-native.js:91:22)
      at _interopRequireWildcard (index.ios.js:14:233)
      at Object.<anonymous> (index.ios.js:14:4)

Here attached is a project demonstrating this, which was produced by taking the react-native init project and adding the single line given above.

ImportRepro.zip

Solution

This seems to occur because Babel transforms the above code into

var _reactNative = require("react-native");
var RN = _interopRequireWildcard(_reactNative);

function _interopRequireWildcard(obj) {
    if (obj && obj.__esModule) {
        return obj;
    } else {
        var newObj = {};
        if (obj != null) {
            for (var key in obj) {
                if (Object.prototype.hasOwnProperty.call(obj, key))
                    newObj[key] = obj[key];
            }
        }
        newObj.default = obj;
        return newObj;
    }
}

I'm guessing that those property accesses are triggering initialization on some of the native modules, which is failing if they're not linked. Worth noting is that using import RN from 'react-native' instead works fine.

I dunno how to fix it. I just talk about things.

Additional Information

  • React Native version: 0.40.0
  • Platform: iOS, and in tests. I haven't tried running on Android, but I would guess it's there too.
  • Operating System: OSX 10.11.6
@quantuminformation
Copy link
Contributor

quantuminformation commented Apr 27, 2017

We just came across this issue today in a fresh install of RN. I think there is some manual linking needed ?

@hramos hramos added the Icebox label Jul 26, 2017
@hramos
Copy link
Contributor

hramos commented Jul 26, 2017

Hi there! This issue is being closed because it has been inactive for a while. Maybe the issue has been fixed in a recent release, or perhaps it is not affecting a lot of people. Either way, we're automatically closing issues after a period of inactivity. Please do not take it personally!

If you think this issue should definitely remain open, please let us know. The following information is helpful when it comes to determining if the issue should be re-opened:

  • Does the issue still reproduce on the latest release candidate? Post a comment with the version you tested.
  • If so, is there any information missing from the bug report? Post a comment with all the information required by the issue template.
  • Is there a pull request that addresses this issue? Post a comment with the PR number so we can follow up.

If you would like to work on a patch to fix the issue, contributions are very welcome! Read through the contribution guide, and feel free to hop into #react-native if you need help planning your contribution.

@hramos hramos closed this as completed Jul 26, 2017
@facebook facebook locked as resolved and limited conversation to collaborators Jul 26, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

4 participants