js에서 import를 하는 방법이 너무 많고 맨날 좀만 틀리면 지랄해대서 이걸 좀 알고 가야곘다.

#
# import

## CommonJS (require)
nodejs의 스탠다드로, Babel등의 transpiler가 필요없이 사용할 수 있다.

``` js
    // 전체 모듈을 로드
    const lodash = require("lodash")
    
    // 특정 함수나 값만을 가져오는 구조분해
    const {map} = require("lodash")

ECMAScript modules (ESM)

ESM은 js에서 모듈 작업을 위한 최신 표준이다. 최신 브라우저와 nodejs v12에서부터 지원된다.

    import express from 'express'
    import { Router } from 'express'
    import * as mathFunctions from './math'
    import express, { Router } from 'express'

difference btw require and import

export

CommomJS ( module.exports, exports )

    module.exports = function myFunction() { ... }
    exports.myFunc = function(){...}
    
    exports.myVariable = 42;

ESM

    export default function myFunc() {...}
    export function myFunction() {...}
    
    export const myVar = 42
    function myFunction(){...}
    
    const myVar = 32;
    
    export { myFunction, myVar }
    function myFunc(){...}
    
    export { myFunc as anotherName }

difference

TS vs JS

TS는 ECMAScript를 따르므로 모듈 import, export는 es6+와 동일하다. 다만, 정적 타입 검사를 추가적으로 하기 때문에 이 점이 영향을 미칠 수 있다.

type import and export

ts에서는 type을 export, import할 수 있다.

    import type { MyType } from "./my-types";
    export type MyType = { ... }