Import-exports.md


javascript์—์„œ๋Š” ํŒŒ์ผ์˜ ๋ชจ๋“ˆํ™”๋ฅผ ์œ„ํ•ด import, export ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

Commonjs

const module1 = require('[๋ชจ๋“ˆ์ด๋ฆ„ / ํ˜„์žฌ์œ„์น˜์—์„œ์˜ ํŒŒ์ผ ์ƒ๋Œ€๊ฒฝ๋กœ]')

module.exports = [๋‚ด๋ณด๋‚ผ ํŒŒ์ผ]

ES6

import

import name form "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1, member2 } from "module-name";
import { member1, member2 as alias2, [...] } from "module-name";
import defaultMember, { member [, [...]] } from "module-name";
import defaultMember, * as alias from "module-name";
import defaultMember from "module-name";
import "module-name";

exports

export { name1, name2, ..., nameN };
export { variable1 as name1, variable2 as name2, ..., nameN };
export let name1, name2, ..., nameN; // ๋˜๋Š” var
export let name1 = ..., name2 = ..., ..., nameN; // ๋˜๋Š” var, const
export expression; export dafault expression;
export default function (...) { ... } // ๋˜๋Š” class, function*
export default function name1(...) { ... } // ๋˜๋Š” class, function*
export { name1 as default, ... };
export * from ...;
export { name1, name2, ..., nameN } from ...;
export { import1 as name1, import2 as name2, ..., nameN } from ...;
  • Named exports

export { myFunction };
export const foo = Math.sqrt(2);

Named exports๋Š” ์—ฌ๋Ÿฌ๊ฐ’์„ export ํ•˜๋Š”๋ฐ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. export ๋œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ import ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Default exports

export default myFunctionOrClass; // ์—ฌ๊ธฐ์—๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์ด ์—†์Šต๋‹ˆ๋‹ค.

๋ชจ๋“ˆ ๋‹น ๋”ฑ ํ•œ ๊ฐœ์˜ default export๋งŒ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. default export๋กœ ๊ฐ์ฒด, ํ•จ์ˆ˜ ํด๋ž˜์Šค ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ฐ€์žฅ ๊ฐ„๋‹จํ•˜๊ฒŒ export ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋”ฑ ํ•œ๊ฐœ๋งŒ default export๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, "๋ฉ”์ธ" ์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„ default export ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

Last updated