Uncaught TypeError: (0 , _module) is not a function

Uncaught TypeError: (0 , _module) is not a function

我正在使用 parcel,并且正在尝试使用 ES6 导入和导出语法。 parcel好像是运行 babel地下的,我对它很陌生。当 openimn 将 index.html 放置在 "dist" 文件夹中时,它无法很好地呈现并在控制台中显示此错误:"Uncaught TypeError: (0 , _module.importedHi) is not a function"

这是导出 JS 文件中的代码:

export const importedHi = document.write("Hello world")

这是 main.js 的代码:

import {importedHi} from "./module1";

importedHi()

这是我在 index.html

中使用的脚本
<script src="js/main.js"></script>

我必须配置什么才能正常工作?

document.writereturnsundefined,所以importedHiundefinedimportedHi()会报错。您可能想要导出调用 document.write 函数 ,例如:

export const importedHi = () => document.write("Hello world");

不过,如果您到了可以使用模块和捆绑器的地步,您可能应该使用更现代的方法来操纵 DOM,例如 createElement / appendChild等等,也许像

export const importedHi = () => {
  document.body.appendChild(document.createTextNode('Hello world'));
};