如何将Webpack输出与Node ESM一起使用?

时间:2018-11-06 21:07:47

标签: javascript webpack node-modules

我有一个从Webpack导出的简单文件。...

class Test{
  constructor(){
    console.log("worked");
  }
}
export { Test }

这会产生...

/*! exports provided: Test */
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony export (binding) *
/ __webpack_require__.d(__webpack_exports__, \"Test\", function() { return Test;
 });\nclass Test{\n  constructor(){\n    console.log(\"worked\");\n  }\n}\n\n\n\
n//# sourceURL=webpack:///./index.js?");

/***/ })

然后我发布并尝试在这样的节点项目中使用它...

import { Test } from "@my/test";
const a = new Test();

但是当我与node --experimental-modules index.mjs一起跑步时,我得到了...

import { Test } from "@my/test";
         ^^^^
SyntaxError: The requested module '@my/test' does not provide an e
xport named 'Test'
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:80:21)

我尝试改用commonjs ...

const x = require("@ccb-praestoanaly/test");
const a = new x.Test();

然后使用node index.js运行,我得到了...

TypeError: x.Test is not a constructor

**更新**

也尝试过这种方法,但是没有用...

class Test{
  constructor(){
    console.log("worked");
  }
}
export default { Test }

0 个答案:

没有答案