discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.4k stars 3.97k forks source link

TypeError: Calling Map constructor without new is invalid w/ React Native web build #2495

Closed Nexuist closed 6 years ago

Nexuist commented 6 years ago

Please describe the problem you are having in as much detail as possible: I'm trying to run discord.js within React Native (0.54.1). Of course, RN has no support for native modules so I figure I would try to use the web builds instead. In theory, this should work. In practice, I encounter the following error:

3:23:57 PM: TypeError: calling Map constructor without new is invalid

This error is located at:
    in App (at registerRootComponent.js:35)
    in RootErrorBoundary (at registerRootComponent.js:34)
    in ExpoRootComponent (at renderApplication.js:35)
    in RCTView (at View.js:78)
    in View (at AppContainer.js:102)
    in RCTView (at View.js:78)
    in View (at AppContainer.js:122)
    in AppContainer (at renderApplication.js:34)
* discord.js:932:24 in Collection
* discord.js:16977:17 in Client
* App.js:8:19 in App
- node_modules/react-proxy/modules/createClassProxy.js:98:23 in <unknown>
- node_modules/react-proxy/modules/createClassProxy.js:96:6 in instantiate
- ... 19 more stack frames from framework internals

FYI here are the problem lines:

class Collection extends Map {
  constructor(iterable) {
    super(iterable);
...

and

this.users = new Collection();

So obviously something fishy is going on here, since this seems like valid JS to me. My guess is that it's a transpilation issue, in which case I'd just like advice on what I should do to make these two things play together. Can I make a web build myself that conforms to the ES version RN wants?

Include a reproducible code sample here, if possible:

import React from "react";
import { StyleSheet, Text, View } from "react-native";
require("./discord.js");

export default class App extends React.Component {
  constructor(props) {
    super(props);
    const client = new Discord.Client();

    client.on("message", msg => {
      const guildTag =
        msg.channel.type === "text" ? `[${msg.guild.name}]` : "[DM]";
      const channelTag =
        msg.channel.type === "text" ? `[#${msg.channel.name}]` : "";
      console.log(`${guildTag}${channelTag} ${msg.author.tag}: ${msg.content}`);
    });

    client.login(API_KEY);
  }
...

Further details:

Thank you!

devsnek commented 6 years ago

@Nexuist babel transforms our es6 class Collection extends Map in such a way that Map is called without new. you'll have to maybe wrap the global map or something similar. this isn't a bug with d.js anyway