onesignal-expo-plugin icon indicating copy to clipboard operation
onesignal-expo-plugin copied to clipboard

Invariant Violation: Native module cannot be null.

Open Giulio64 opened this issue 2 years ago • 20 comments

Hello everybody, I'm trying to add one-signal to my expo managed project, In the App.js file I have:

export default function App() {
  useEffect(() => {
   ///Other things
    OneSignal.setAppId(Constants.manifest.extra.oneSignalAppId);
  }, []);

When trying to run the application on iOS I get:

Invariant Violation: Native module cannot be null.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:204:6 in guardedLoadModule
at http://127.0.0.1:19000/node_modules/expo/AppEntry.bundle?platform=ios&dev=true&hot=false&minify=false:263808:3 in global code

Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError

Here is my package.json:

{
  "name": "ctsapp",
  "version": "1.0.0",
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@fortawesome/free-solid-svg-icons": "^5.15.4",
    "@fortawesome/react-native-fontawesome": "^0.2.7",
    "@react-native-async-storage/async-storage": "~1.15.0",
    "@react-navigation/bottom-tabs": "^6.0.9",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.5",
    "accordion-collapse-react-native": "^1.1.0",
    "axios": "^0.25.0",
    "expo": "~44.0.0",
    "expo-av": "~10.2.0",
    "expo-barcode-scanner": "~11.2.0",
    "expo-constants": "~13.0.1",
    "expo-location": "~14.0.1",
    "expo-status-bar": "~1.2.0",
    "expo-web-browser": "~10.1.0",
    "firebase": "^9.6.4",
    "geolib": "^3.3.3",
    "jsbarcode": "^3.11.5",
    "onesignal-expo-plugin": "^1.0.0-beta10",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-gesture-handler": "~2.1.0",
    "react-native-image-zoom-viewer": "^3.0.1",
    "react-native-svg": "12.1.1",
    "react-native-swipe-list-view": "^3.2.9",
    "react-native-web": "0.17.1",
    "react-native-webview": "11.15.0"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9"
  },
  "private": true
}

Any suggestions? Thanks

Giulio64 avatar Jan 31 '22 20:01 Giulio64

Howdy, You need to add the SDK:

yarn add react-native-onesignal

Enjoy!

rgomezp avatar Jan 31 '22 21:01 rgomezp

Howdy, You need to add the SDK:

yarn add react-native-onesignal

Enjoy!

@rgomezp It isn't solve my problem. react-native-onesignal added but it still gives an error.

App.js:

import React, { useEffect } from 'react';
import Bootstrap from './hooks/Bootstrap';
import RootNavigation from './stacks/RootStack';
import { Provider } from 'react-redux';
import { store } from './redux/Store';
import Constants from 'expo-constants';
import OneSignal from 'react-native-onesignal';

OneSignal.setAppId(Constants.manifest.extra.oneSignalAppId);

const App = () => {

  const initFunc = (init) => {
    return <RootNavigation init={init} />;
  };

  return (
    <Provider store={store}>
      <Bootstrap initFunc={initFunc} />
    </Provider>
  );
};

export default App;

Here is my dependencies:

"dependencies": {
    "@clouxart/react-native-circle-timer": "^2.5.7",
    "@clouxart/react-native-countdown-circle-timer": "2.6.2",
    "@expo/config-plugins": "^4.0.16",
    "@expo/vector-icons": "^12.0.5",
    "@react-native-async-storage/async-storage": "^1.15.7",
    "@react-navigation/native": "^6.0.2",
    "@react-navigation/native-stack": "^6.1.0",
    "@sentry/react-native": "^3.2.6",
    "@unimodules/core": "~7.2.0",
    "axios": "^0.24.0",
    "date-fns": "^2.23.0",
    "dotenv": "^10.0.0",
    "expo": "^43.0.0",
    "expo-apple-authentication": "~4.0.3",
    "expo-application": "~4.0.0",
    "expo-av": "~10.1.3",
    "expo-constants": "~12.1.3",
    "expo-device": "^4.1.1",
    "expo-facebook": "12.0.3",
    "expo-font": "~10.0.3",
    "expo-google-app-auth": "~9.0.0",
    "expo-google-sign-in": "~10.0.3",
    "expo-linking": "~2.4.2",
    "expo-notifications": "~0.13.3",
    "expo-secure-store": "~11.0.3",
    "expo-splash-screen": "~0.13.5",
    "expo-status-bar": "~1.1.0",
    "expo-updates": "~0.10.15",
    "jwt-decode": "^3.1.2",
    "moment": "^2.29.1",
    "onesignal-expo-plugin": "^1.0.0-beta10",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-native": "0.64.3",
    "react-native-calendar-picker": "^7.1.2",
    "react-native-collapsible": "^1.6.0",
    "react-native-onesignal": "^4.3.6",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.8.0",
    "react-native-svg": "12.1.1",
    "react-native-swipe-gestures": "^1.0.5",
    "react-native-unimodules": "~0.15.0",
    "react-native-uuid": "^2.0.1",
    "react-native-web": "0.17.1",
    "react-native-webview": "11.13.0",
    "react-redux": "^7.2.4",
    "redux": "^4.1.1",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.3.0",
    "sentry-expo": "^4.0.0"
  },

berkaygurbuz avatar Feb 09 '22 12:02 berkaygurbuz

Howdy, You need to add the SDK:

yarn add react-native-onesignal

Enjoy!

Hello, thanks for the reply! Unfortunately now I'm getting:

iOS Bundling failed 3417ms
Unable to resolve module fs from /Users/giulioserra/Documents/App/CTS/node_modules/onesignal-expo-plugin/build/onesignal/withOneSignalIos.js: fs could not be found within the project or in these directories:
  node_modules

If you are sure the module exists, try these steps:
 1. Clear watchman watches: watchman watch-del-all
 2. Delete node_modules and run yarn install
 3. Reset Metro's cache: yarn start --reset-cache
 4. Remove the cache: rm -rf /tmp/metro-*
  29 | exports.xcodeProjectAddNse = exports.withOneSignalIos = void 0;
  30 | const config_plugins_1 = require("@expo/config-plugins");
> 31 | const fs = __importStar(require("fs"));

Already cleared my cache, still I'm getting the error above.

Giulio64 avatar Feb 10 '22 12:02 Giulio64

I have a similiar error with 42 expo version

Could not load RNOneSignal native module. Make sure native dependencies are properly linked. at node_modules/react-native-onesignal/dist/helpers.js:15:8 in isNativeModuleLoaded at node_modules/react-native-onesignal/dist/index.js:61:13 in OneSignal.setAppId at App.js:13:4 in useEffect$argument_0 at node_modules/react-native/Libraries/ReactNative/renderApplication.js:54:4 in renderApplication at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:117:25 in runnables.appKey.run at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:202:4 in runApplication

brunolcarlos avatar Jun 24 '22 20:06 brunolcarlos

Hello. Please make sure you are running in a supported environment. Example: Expo Go is not a supported environment. See the repo README for more details

rgomezp avatar Jun 24 '22 21:06 rgomezp

I created a build and still not working

brunolcarlos avatar Jun 24 '22 22:06 brunolcarlos

Please provide more information regarding your build so I can help you.

rgomezp avatar Jun 24 '22 23:06 rgomezp

I also had the same issue with onesignal. To use onesignal expo plugin, you have to eject your project using expo eject. I don't recommend the eject project but there is no solution.

Your Expo project needs a native module that Expo doesn't currently support. We're always expanding the Expo SDK, so we hope this is never the case. But it happens, especially if your app has very specific and uncommon native demands.

More information about ejecting click here.

berkaygurbuz avatar Jun 28 '22 08:06 berkaygurbuz

I'm having the same issue now. Is this a common issue everyone has from expo? Or it's due to our environment?

alexander01202 avatar Jul 02 '22 16:07 alexander01202

I have a similiar error with 42 expo version

Could not load RNOneSignal native module. Make sure native dependencies are properly linked. at node_modules/react-native-onesignal/dist/helpers.js:15:8 in isNativeModuleLoaded at node_modules/react-native-onesignal/dist/index.js:61:13 in OneSignal.setAppId at App.js:13:4 in useEffect$argument_0 at node_modules/react-native/Libraries/ReactNative/renderApplication.js:54:4 in renderApplication at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:117:25 in runnables.appKey.run at node_modules/react-native/Libraries/ReactNative/AppRegistry.js:202:4 in runApplication

Hello I am having the same issue. image image

rubendmatos1985 avatar Jul 10 '22 19:07 rubendmatos1985

Howdy, As a reminder, these are the supported environments.

The Expo run commands (expo run:[android|ios]) Custom clients EAS Build

Please make sure you are building and running in one of them. The Expo Go app is not a supported environment.

Also, please share your Expo config file.

While I don't suspect it is the cause of the issue, perhaps try putting OneSignal as the first plugin in your plugin list.

Furthermore, if you have an app you could share reproducing the issue, please share the repo link or message the OneSignal customer support team via our chat interface at onesignal.com

rgomezp avatar Jul 14 '22 23:07 rgomezp

Hi All, I have similar issue in an Expo Managed Workflow (expo 44) with EAS Build. OneSignal works on Managed Workflow ? Because here it says: Our new plugin streamlines the process of setting up OneSignal in an Expo application within a managed workflow environment. This means developers no longer need to "eject" into a bare workflow to integrate, test, and make changes to a OneSignal integration. Those making use of Expo's Cloud Build Service (EAS) can now take full advantage of OneSignal without workflow interruption.

This is my app.config.file:

      "extra": {
        "env": CURRENT_ENV,
        "restart": APP_RESTART,
        "oneSignalAppId": currentEnvDetails.ONE_SIGNAL.APP_ID
      },
      "plugins": [
        [
          "onesignal-expo-plugin",
          {
            "mode": currentEnvDetails.ONE_SIGNAL.MODE,
          }
        ]
      ],

This is my package.json:

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject",
    "dev": "cross-env APP_VARIANT=development expo start",
    "test": "cross-env APP_VARIANT=test expo start",
    "prod": "cross-env APP_VARIANT=production expo start",
    "build": "node build_script/start.js",
    "build_wave": "node build_script/start_wave.js",
    "_COMMENT_START": "START - DA ELIMINARE NNO APPENA PRONTI GLI SCRIPT PER WAVE",
    "publish-development": "cross-env APP_VARIANT=development expo publish --release-channel development-testing",
    "publish-test": "cross-env APP_VARIANT=test expo publish --release-channel test",
    "publish-production": "cross-env APP_VARIANT=production expo publish --release-channel prod",
    "_COMMENT_END": "END - DA ELIMINARE NNO APPENA PRONTI GLI SCRIPT PER WAVE",
    "publish-wave2-development": "cross-env APP_VARIANT=development expo publish --release-channel development-testing",
    "publish-wave2-test": "cross-env APP_VARIANT=test expo publish --release-channel test",
    "publish-wave2-production": "cross-env APP_VARIANT=production expo publish --release-channel prod",
    "publish-wave3-development": "cross-env APP_VARIANT=development-wave3 expo publish --release-channel development-wave3-testing",
    "publish-wave3-test": "cross-env APP_VARIANT=test-wave3 expo publish --release-channel test-wave3",
    "publish-wave3-production": "cross-env APP_VARIANT=production-wave3 expo publish --release-channel prod-wave3",
    "_COMMENT_BUILD_START": "START - DA ELIMINARE NNO APPENA PRONTI GLI SCRIPT PER WAVE",
    "build-development-ios": "eas build --platform ios --profile development",
    "build-test-ios": "eas build --platform ios --profile test",
    "build-production-ios": "eas build --platform ios --profile production",
    "_COMMNET_BUILD_END": "END - DA ELIMINARE NNO APPENA PRONTI GLI SCRIPT PER WAVE",
    "build-wave2-development-ios": "eas build --platform ios --profile development",
    "build-wave2-test-ios": "eas build --platform ios --profile test",
    "build-wave2-production-ios": "eas build --platform ios --profile production",
    "build-wave3-development-ios": "eas build --platform ios --profile development-wave3",
    "build-wave3-test-ios": "eas build --platform ios --profile test-wave3",
    "build-wave3-production-ios": "eas build --platform ios --profile production-wave3",
    "build-development-android": "eas build --platform android --profile development",
    "build-test-android": "eas build --platform android --profile test",
    "build-production-android": "eas build --platform android --profile production"
  },
  "dependencies": {
    "@eng-fashion-moncler/monclient-app-components": "^0.3.23",
    "@eng-fashion-moncler/monclient-app-services": "0.3.19",
    "@expo/vector-icons": "^12.0.0",
    "@miblanchard/react-native-slider": "^2.1.0",
    "@react-native-async-storage/async-storage": "~1.15.0",
    "@react-native-community/datetimepicker": "4.0.0",
    "@react-native-picker/picker": "2.2.1",
    "@react-navigation/bottom-tabs": "^6.0.9",
    "@react-navigation/native": "^6.0.6",
    "@react-navigation/native-stack": "^6.2.5",
    "@react-navigation/stack": "^6.0.11",
    "cheerio": "^1.0.0-rc.12",
    "expo": "^44.0.0",
    "expo-camera": "~12.1.2",
    "expo-clipboard": "~2.1.0",
    "expo-device": "~4.1.0",
    "expo-firebase-analytics": "~6.0.0",
    "expo-font": "~10.0.4",
    "expo-image-manipulator": "~10.2.0",
    "expo-image-picker": "~12.0.1",
    "expo-linear-gradient": "~11.0.3",
    "expo-localization": "~12.0.0",
    "expo-notifications": "~0.14.0",
    "expo-secure-store": "~11.1.0",
    "expo-sensors": "~11.1.0",
    "expo-status-bar": "~1.2.0",
    "expo-updates": "~0.11.7",
    "i18next": "^21.6.10",
    "inquirer": "^8.2.4",
    "lodash": "^4.17.21",
    "lottie-react-native": "5.0.1",
    "moment": "^2.29.1",
    "nodemailer": "^6.7.6",
    "onesignal-expo-plugin": "^1.0.2",
    "react": "17.0.1",
    "react-dom": "17.0.1",
    "react-i18next": "^11.15.3",
    "react-native": "0.64.3",
    "react-native-calendars": "^1.1278.0",
    "react-native-gesture-handler": "~2.1.0",
    "react-native-json-tree": "^1.3.0",
    "react-native-mask-input": "^1.2.0",
    "react-native-modal": "12.1.0",
    "react-native-onesignal": "^4.3.11",
    "react-native-safe-area-context": "3.3.2",
    "react-native-screens": "~3.10.1",
    "react-native-shimmer-placeholder": "^2.0.8",
    "react-native-snap-carousel": "^4.0.0-beta.6",
    "react-native-svg": "12.1.1",
    "react-native-svg-transformer": "^0.14.3",
    "react-native-swiper": "^1.6.0",
    "react-native-use-timestamp": "^2.0.0",
    "react-native-web": "0.17.1",
    "react-redux": "^7.2.6",
    "redux": "^4.1.2"
  },
  "devDependencies": {
    "@babel/core": "^7.12.9",
    "cross-env": "^7.0.3",
    "prop-types": "^15.8.1"
  },
  "private": true
}

ilgiapa avatar Jul 21 '22 13:07 ilgiapa

@ilgiapa Are you getting the same error or a different error? Can you please include logs?

Furthermore, if you eject, do you still see the error?

Is your project a mono-repo / are you using npm module hoisting?

Are you able to share a project with us?

Can you run expo prebuild and compare the results?

rgomezp avatar Jul 21 '22 20:07 rgomezp

Hi @rgomezp

Same error. At this moment I can't eject. Mono Repo project. I tried to share a mini project later.

What I've done are the steps in this link.

expo install onesignal-expo-plugin
npm install --save react-native-onesignal

export default {
  ...
  plugins: [
    [
      "onesignal-expo-plugin",
      {
        mode: "development",
      }
    ]
  ]
};


{
  "extra": {
    "oneSignalAppId": "<YOUR APP ID HERE>"
  }
}

And then

import OneSignal from 'react-native-onesignal';
import Constants from "expo-constants";
OneSignal.setAppId(Constants.manifest.extra.oneSignalAppId);
Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
at node_modules/expo/build/logs/LogSerialization.js:156:14 in _captureConsoleStackTrace
at node_modules/expo/build/logs/LogSerialization.js:41:26 in serializeLogDataAsync
- ... 9 more stack frames from framework internals

Invariant Violation: Native module cannot be null.
at node_modules/expo/build/logs/LogSerialization.js:156:14 in _captureConsoleStackTrace
at node_modules/expo/build/logs/LogSerialization.js:41:26 in serializeLogDataAsync
- ... 9 more stack frames from framework internals

Schermata 2022-07-22 alle 08 05 09

ilgiapa avatar Jul 22 '22 06:07 ilgiapa

Also getting this issue when trying to skip the dependency for Expo Go

jorgeruvalcaba avatar Jul 23 '22 01:07 jorgeruvalcaba

Hi All,

After re-created the Push Certificate and released by EAS BUILD, the App works and push notifications arrive

On Expo Go App or Simulator or "expo start" does not work. In order to run in dev mode it's necessary to import dynamically this line:

import OneSignal from 'react-native-onesignal';

Because this line BREAKS THE APP

Gianluca,

ilgiapa avatar Aug 05 '22 12:08 ilgiapa

Howdy, y'all, That is correct. It does not work in Expo Go.

If you're running in vanilla react native, you would need import OneSignal from 'react-native-onesignal'; and if you're running RN via Expo, you need both the SDK package as well as the Expo config plugin (this package).

Separately, the iOS Simulator doesn't work either, but that is a limitation of the simulator since it doesn't support push notifications.

Please make sure you are running in a supported environment as listed on the README of this repository.

rgomezp avatar Aug 08 '22 21:08 rgomezp

Hi All,

After re-created the Push Certificate and released by EAS BUILD, the App works and push notifications arrive

On Expo Go App or Simulator or "expo start" does not work. In order to run in dev mode it's necessary to import dynamically this line:

import OneSignal from 'react-native-onesignal';

Because this line BREAKS THE APP

Gianluca,

Howdy, y'all, That is correct. It does not work in Expo Go.

If you're running in vanilla react native, you would need import OneSignal from 'react-native-onesignal'; and if you're running RN via Expo, you need both the SDK package as well as the Expo config plugin (this package).

Separately, the iOS Simulator doesn't work either, but that is a limitation of the simulator since it doesn't support push notifications.

Please make sure you are running in a supported environment as listed on the README of this repository.

When Expo Go will be supported??

henriquearakaki avatar Aug 29 '22 18:08 henriquearakaki

@henriquearakaki Expo Go does not allow you to add custom native code, as a portion of the OneSignal SDK is native we do not anticipate the Expo Go environment will be supported.

brismithers avatar Aug 30 '22 18:08 brismithers

Hi All,

After re-created the Push Certificate and released by EAS BUILD, the App works and push notifications arrive

On Expo Go App or Simulator or "expo start" does not work. In order to run in dev mode it's necessary to import dynamically this line:

import OneSignal from 'react-native-onesignal';

Because this line BREAKS THE APP

Gianluca,

@ilgiapa how did you import this line dynamically?

jorgeruvalcaba avatar Aug 31 '22 23:08 jorgeruvalcaba

Hey folks! From looking through the history it looks like there were 2 different issues raised in this ticket:

  1. Please make sure you run in a supported environment.
  2. Support for monorepos was added as of v1.1.1

I'm going to close this issue but if there are any additional problems please raise them up and we will take a look. Thanks so much!

brismithers avatar Sep 15 '22 14:09 brismithers