Meteor shell and es6: namespacing issue


#1

I have a block in my code using NPM, therefore, it is required on the server.

/**
* Basic idea of what is going on
* this needs Npm so it's on the server.
*/
var fs = Npm.require('fs');
var net = Npm.require('net');

class Cylon {
  constructor(host='127.0.0.1', port='8080') {
    this.connection = {
      host: host,
      port: port
    };

    this.sockets = {};
    this._connect('messages');
  }

  _connect(name) {
    this.sockets[name] = new net.Socket();
  }
}

I then open meteor shell and try to use this.

> var cylon = new Cylon();
ReferenceError: Cylon is not defined

when using something on the client, you can use this “hack” to get around it

class MyClass { /** ... */ }
window.MyClass = MyClass;

Can you do similar on the server? How do you access the classes in the best way possible?


#2

You can make the class global:

class CylonClass = {...}
Cylon = CylonClass;

Now Cylon is global.