首页 > 解决方案 > export default 和 module.exports 有什么区别

问题描述

在 node.js 中,当我导出一个函数时,将它导入另一个文件的正确方法是什么?

https://nodejs.org/docs/latest-v9.x/api/

名称.js:

function Name(){}

module.exports = Name;

index.js:

const Name = require('./Name');

名称.js:

function Name(){}

export default Name;

index.js:

const Name = require('./Name');

正确的方法是什么?并按原样应用解构?

标签: node.jsexpress

解决方案


Do the first one with module.exports. This is the node.js way to do it.

The second why is more of an ES6 / commonJS alternative.

Again do the first one.


推荐阅读