Meteor Package Can't Import/Export Client Modules

I have a testing package that has client-side and server-side functions to use in my Mocha tests. Server-side code is in packages/package-name/server/functions.js and client-side code is in packages/package-name/client/functions.js. The server-side tests work fine, but the client-side tests give me an error about import being a reserved keyword.

W20170214-16:41:52.524(-6)? (STDERR) import { Meteor } from 'meteor/meteor';
W20170214-16:41:52.525(-6)? (STDERR) ^^^^^^
W20170214-16:41:52.525(-6)? (STDERR) 
W20170214-16:41:52.525(-6)? (STDERR) SyntaxError: Unexpected reserved word

Why can’t I import/export the client-side modules in my package?

have you included the ecmascript package in your package dependencies?

1 Like

Yeah, ecmascript is included. Importing the server module wouldn’t work if it weren’t.

I got this to work by using CommonJS instead of Ecmascript. So, anytime I would have said something like:

import { Meteor } from 'meteor/meteor'

I instead said:

var Meteor = require('meteor/meteor').Meteor

This ended up working out really well because it allowed me to conditionally import modules based on whether the package was run by the client or server. I could export modules with:

var someObj = { /* object to export */ };
module.exports.someObj = someObj;

var someFunc = function() { /* function to export */ };
module.exports.someFunc = someFunc;