question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

React Native, Invariant Violation: Native module cannot be null.

See original GitHub issue

šŸ› Bug Report

hey, hi! i have a similar issue (i think) here: https://github.com/facebook/jest/issues/2208, but this issue is quite old, and i have followed the solutions there, but none worked for me.

at first, i encountered this error for running jest:

/home/nrion/Desktop/mobile-ui/PriceInsight_app/node_modules/react-native-onesignal/index.js:4
    import { NativeModules, NativeEventEmitter, NetInfo, Platform } from 'react-native';
    ^^^^^^
    
    SyntaxError: Unexpected token import

    > 1 | import OneSignal from 'react-native-onesignal';
      2 | 
      3 | export const STORE_TIMEFRAME = 'STORE_TIMEFRAME';
      4 | export const STORE_TIMEFRAMES = 'STORE_TIMEFRAMES';
      
      at ScriptTransformer._transformAndBuildScript (../../../../../usr/lib/node_modules/jest-cli/node_modules/jest-runtime/build/script_transformer.js:316:17)
      at Object.<anonymous> (src/actions/subscription.js:1:387)
      at Object.<anonymous> (src/reducers/subscription.js:1:402)

so i went ahead and added react-native-onesignal to package.json as such:

"transformIgnorePatterns": [
      "node_modules/(?!(react-native-onesignal|react-native-login|react-native-elements)/)"
    ]

so that solved my problem. but here comes another problem:

 Invariant Violation: Native module cannot be null.

    > 1 | import OneSignal from 'react-native-onesignal';
      2 | 
      3 | export const STORE_TIMEFRAME = 'STORE_TIMEFRAME';
      4 | export const STORE_TIMEFRAMES = 'STORE_TIMEFRAMES';
      
      at invariant (node_modules/fbjs/lib/invariant.js:42:15)
      at new NativeEventEmitter (node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:37:31)
      at Object.<anonymous> (node_modules/react-native-onesignal/index.js:9:33)

i tried mocking OneSignal, but this is what happens when I do that:

 Cannot find module 'OneSignal' from 'Main.test.js'

    > 1 | import React from 'react'
      2 | import { shallow } from '../../jest-setup'
      3 | import configureStore from 'redux-mock-store'
      4 | import OneSignal from 'react-native-onesignal'
      
      at Resolver.resolveModule (../../../../../usr/lib/node_modules/jest-cli/node_modules/jest-resolve/build/index.js:169:17)
      at Object.<anonymous> (src/__tests__/Main.test.js:1:108)

To Reproduce

Steps to reproduce the behavior:

  1. npm install react-native-onesignal
  2. run jest

Expected behavior

test passes, writes a snapshot

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

5reactions
thymikeecommented, Jun 2, 2018

Oh, you need to mock the react-native-onesignal, not OneSignal. Mocking is for modules and OneSignal is not a module - itā€™s a variable name.

jest.mock('react-native-onesignal', () => {
  return {
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    inFocusDisplaying: jest.fn()
  }
})

should do the trick

2reactions
Pallavisobacommented, Feb 13, 2020

Hi, Iā€™m a having an issue while running my app ā€œInvariant Violation: Native module cannot be nullā€

Can someone please help for that Thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invariant Violation: Native module cannot be null #43 - GitHub
At iOS, it builds successfully, however, when the app starts, I get this error: Invariant Violation: Native module cannot be null.
Read more >
React-native, "Native module cannot be null" - Stack Overflow
To solve this, your Project Navigator -> Target -> General -> Linked Frameworks and Libraries, make sure somelib.a is there. enter image description...
Read more >
Invariant Violation: Native module cannot be null. on Managed ...
SDK Version: 41.0.0; Platforms(Android/iOS/web/all): Android/ios. I upgraded to SDK 41 yesterday and worked out some smaller issues beforeĀ ...
Read more >
" has not been registered" and "Invariant Violation: Native ...
I am working on a project in React Native using Expo and Firebase. ... and "Invariant Violation: Native module cannot be null" errorsĀ ......
Read more >
React Native Ios: Native Module cannot be null - iTecNote
This usually happens when you fail to link the third lib to your target causing NativeModules find nothing at attempt. To solve this,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found