5

How do I require the events Node module within my React Native project? I ran into some module dependency issues with util and http which I resolved by using Browserify to package.

I try to take the same approach with events:

npm install events
var EventEmitter = require('events').EventEmitter;

After packaging with Browserify, I still get the same error from React Native: "Requiring unknown module 'events'."

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
mdunkle
  • 1,715
  • 2
  • 14
  • 17

3 Answers3

2

This is what you want: https://github.com/facebook/react-native/issues/1058

var EventEmitter = require('EventEmitter');
Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Logan
  • 41
  • 6
1

Create a file EventEmitter.js

const EventEmitter = require('events')

const emitter = new EventEmitter()

export default emitter

Import it into your component:

import EventEmitter from '@/lib/EventEmitter'

EventEmitter.on('example', this.do_this)
Andrew
  • 227,796
  • 193
  • 515
  • 708
0

I'm using events and a bunch of other node core modules by using react-native-webpack-server. It takes a bit of setup, but otherwise you're going to be running into the same problem with Buffer, assert, util, crypto, etc. Better take care of them all in one go.

Mark Vayngrib
  • 974
  • 5
  • 14