0

I'm trying to import a package that I've written into another package that I've written.

Pre-Babel Loader

class TestClass {
    constructor() {
        // Load flags on import here
        console.log("TESTING CONSTRUCTOR");
    }

    log(message) {
        console.log("TESTING LOG");
    }
}

export default new TestClass();

Post Babel Loader

var TestClass = function () {
    function TestClass() {
        _classCallCheck(this, TestClass);

        // Load flags on import here
        console.log("TESTING CONSTRUCTOR");
    }

    _createClass(TestClass, [{
        key: "log",
        value: function log(message) {
            console.log("TESTING LOG");
        }
    }]);

    return TestClass;
}();

exports.default = new TestClass();

The import itself is simply a import TestClass from 'testclass-js'. However, every single time I'm trying to load it I get a "Darklaunch is not defined" error, and can't call any of the methods of the class.

I'm wondering what I've done wrong here.

Jake
  • 225
  • 1
  • 2
  • 12

1 Answers1

0

If you're trying to import the ES5/commonjs version, you'll need to import 'yourmodule'.default; This change came in around babel 6

Babel 6 changes how it exports default

Read more on the original issue: https://github.com/babel/babel/issues/2212

If your package contains both an es5 and es6+ version, you can point to the es6 version in the module key in package.json and webpack/rollup will pick that up and bundle it instead of the commonjs version

3stacks
  • 1,880
  • 1
  • 16
  • 21
  • I've tried require but it seems to be undefined. I'd have to load require.js into my package I presume. I'll dig into the original issue and see what I find, thanks. – Jake Mar 01 '18 at 02:27
  • Seeing that it says `exports.default`, you can assume that exports is an object that would either have a key for each named export or just default. const yourThing = require('your-module').default Would work, but annoying to not use imports. Does that at least work as a start? – 3stacks Mar 01 '18 at 02:32
  • require is undefined since I'm using this in the browser. Doesn't work just straight up. – Jake Mar 01 '18 at 02:37
  • Ah, are you not using a bundler? like sourcetype="module" and just importing natively? If so you may need your modules to be using the ES6 exports too, I've never tried using babelified modules straight in browser – 3stacks Mar 01 '18 at 02:39
  • It's an odd setup. I'm doing React SSR with modules inside it, bundled by webpack. – Jake Mar 01 '18 at 02:51