简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM