How to create NPM packages that work on client and server for meteor?

Let’s say I want to have a very simple package. It will only extend a normal Socket.

import {Socket} from 'net';
export class SpecialSocket extends Socket {
  constructor() {
    super(...arguments);
  }
  connect() {
    return new Promise((resolve, reject) => {
      this.once('connect', resolve).once('error', reject);
      super.connect(...arguments);
    });
  }
}

Now I want to be able to have this on the client AND the server, but on the client, it will just make a promisified call to the server that returns when it is done.

How is this done with NPM and meteor?

Meteor supports the browser field that’s also used by Browserify and Webpack:

So basically you can just have your package load different code on the client by setting:

browser: './path/to/browser-version.js'
1 Like

Perfect! Thank you Sashko

1 Like