본문으로 바로가기

Node.js require vs ES2015 import/export

category Coding 2017. 7. 7. 17:56

Node.js에서 사용할 수 있는 module system:

  • Importing modules using require, and exporting using module.exports and exports.foo
  • Importing modules using ES6 import, and exporting using ES6 export.

그럼 둘 간의 차이는?

require() from NodeJS (CommonJS)

  • dynamic loading을 할 수 있다.
  • synchronous (여러 개를 require하면 순차적으로 처리)

import from ES6

  • named import를 사용할 수 있고, import하는 대상을 온전히 로딩하는게 아니고, 그 중 필요한 부분만 destructuring처럼 가져올 수 있어서 메모리에 장점
  • asynchronous 


현재는 Babel을 통해서 import/export를 사용하는데 이것들은 기본적으로 CommonJS (require/module.exports) 로 transpiling되기 때문에 CommonJS방식을 사용하는 것도 문제가 되진 않겠다.




참고 출처: http://researchhubs.com/post/computing/javascript/nodejs-require-vs-es6-import-export.html