简体   繁体   English

编写ES6模块并将其转换为CommonJS时,实际上未导出任何指定的导出

[英]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. 我有一个ES6模块,试图从中导出几个功能。 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. 最终目标是导入到purescript中,但是现在我所谓的导出函数甚至都没有出现在Node中,我也不明白为什么。

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和Node中。 Purescript reports that point and pointToString are not defined. Purescript报告未定义pointpointToString The Node session looks like this: Node会话如下所示:

> 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-cli以CommonJS格式创建一个适用于Node的模块:

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

It's generally a good idea to put compiled files into a separate directory. 通常将编译文件放入一个单独的目录是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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