节点v12:导出时未定义模块



我在index.js 中导出一个函数

module.exports = {
myFunc
};

然后我运行node index.js我得到的错误

module.exports = {
^
ReferenceError: module is not defined

在节点版本12中,有什么新的导出方法吗?

看起来您的应用程序正在使用es模块,这意味着您需要使用export/import而不是module.exports/require。以下是修复方法:

const myFunc = () => {
console.log("test");
}
export {myFunc};

然后,您可以使用以下命令import该函数:

import {myFunc} from './index.js'
myFunc();

最新更新