5

Hey I am doing a small project using react-app and I have been trying all day to create export for this module.

I have installed it with npm, and i want to edit it so i can import and use it in my app.js

I have tried to define "reddit" using class\function\let and use either:

export default
module.exports

And either

 import reddit from 'reddit.js';
 var reddit = require('reddit.js');

And trying to check with a simple function from the module:

console.log(reddit.hot('cats'));

But I am still getting:

Uncaught TypeError: reddit.hot is not a function

I am a bit lost, what am I doing wrong?

Elydasian
  • 2,016
  • 5
  • 23
  • 41
Ben Porat
  • 583
  • 1
  • 4
  • 6

2 Answers2

1

The module uses window.reddit global variable. Don't override it! So if you are on client side, just use:

require('reddit.js')
//...
reddit.hot('cats')

For server side - you have to do some trick to make it work, because on server side you don't have 'window' global variable.

Upd:

Example of back end usage:

const window = {};
const r = require('./reddit.js') // You don't really use r 
const reddit = window.reddit;

reddit.hot('cats')
Oleg Imanilov
  • 2,591
  • 1
  • 13
  • 26
  • On the server-side, they can set `window = global`. – Pedro Castilho May 02 '17 at 17:29
  • with or without var reddit = ? without i am getting "Failed to compile error 'reddit' is not defined no-undef" from npm start with i am still getting reddit.hot is not a function – Ben Porat May 02 '17 at 17:57
0

Reddit does not export anything because it was mainly designed to be added as a script tag!

// So for CommonJS module:
require('reddit.js');

//And for ES6 modules
import 'reddit.js';

And you you will be able to access reddit and reddit.hot() method through the window object.

bigfanjs
  • 754
  • 3
  • 7
  • 17
  • Thanks import 'reddit.js'; let reddit; reddit= window.reddit; console.log(reddit.hot('cats')); solved it. – Ben Porat May 02 '17 at 18:05