How to import a module from a Meteor package?


#1

I have a Meteor package in which I am exporting a couple of modules and I would like to use the import syntax rather than having them exist globally. Here is what I am doing in the package.js file.

Package.describe({
  name: 'mycompany:mypackage',
  ...
});

Package.onUse(function(api) {
...
api.export('mod1', 'server');
...
}

Here is the mod1.js file.

mod1 = {};

mod1.foo = function() { console.log("foo called"); }

module.export = mod1

I can call mod1.foo in my application code since it’s a global but I would like use the import syntax. If I use this import line

import mod1 from "meteor/mycompany:mypackage/mod1"

mod1 is an empty object.


#2

Instead of using api.export you could just leverage Meteor 1.3’s ES2015 support and export from your package like:

export default mod1;

You could then import your package using:

import mod1 from 'meteor/mycompany:mypackage';

Take a look at the package here for a quick example.


#3

This was very helpful, thank you.