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'."

7
  • Is this running on nodejs or in a browser? Commented Jul 23, 2015 at 4:05
  • It's actually running in React Native's environment: facebook.github.io/react-native/docs/… Commented Jul 23, 2015 at 4:10
  • My guess is because events requires the function call as part of the initializer, you're going to run into trouble. Maybe try var event = require('events'); eventEmitter = event.EventEmitter;? Commented Jul 23, 2015 at 4:12
  • Yep, bundle file has been created. Works great with util and http. Commented Jul 23, 2015 at 4:16
  • Still no luck with var event = require('events'); eventEmitter = event.EventEmitter; Commented Jul 23, 2015 at 4:16

3 Answers 3

2

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

var EventEmitter = require('EventEmitter');
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.