Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define export for a module in javascript?

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?

like image 989
Ben Porat Avatar asked Nov 07 '22 22:11

Ben Porat


1 Answers

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')
like image 77
Oleg Imanilov Avatar answered Nov 14 '22 23:11

Oleg Imanilov