Search code examples
javascriptnode.jsecmascript-6purescript

When writing an ES6 module and converting to CommonJS, no specified exports are actually being exported


I have an ES6 module from which I'm trying to export several functions. End goal is to import into purescript, but right now my supposedly exported functions don't even appear in Node, and I cannot understand why.

Here is the module:

"use strict";

const point = (x, y) => new Point(x, y)
const pointToString = (point) => point.toString()

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    toString () {
        return `(${this.x}, ${this.y})`;
    }
}

export { point, pointToString }

I transpile it like so:

browserify src/page.js -t [ babelify --presets [es2015] ] > src/Page.js

And then I try to load it into Purescript and Node. Purescript reports that point and pointToString are not defined. The Node session looks like this:

> require("./src/Page.js")
{}
> require("./src/Page.js").point
undefined
> require("./src/Page.js").pointToString
undefined
> 

I'm at a total loss. What am I supposed to be doing to get those two functions to export?


Solution

  • Use the babel-cli to create a module in CommonJS format what is suitable for the Node:

    babel --presets=es2015 src/page.js > lib/Page.js

    It's generally a good idea to put compiled files into a separate directory.