๐Ÿšจ'React' refers to a UMD global, but the current file is a module.๐Ÿšจ

ํ˜œํ˜œยท2024๋…„ 10์›” 21์ผ
0

Error Note

๋ชฉ๋ก ๋ณด๊ธฐ
18/19
post-thumbnail

โ—๋ฌธ์ œ ๋‚ด์šฉ

์ง์—ญํ•˜์ž๋ฉด 'React'๋Š” UMD global์„ ์ฐธ์กฐํ•˜์ง€๋งŒ, ํ˜„์žฌ ํŒŒ์ผ์€ ๋ชจ๋“ˆ์ด๋ผ๋Š” ๋œป.

โš’๏ธ ๋ฌธ์ œ ํ•ด๊ฒฐ

๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ

import React from 'react';

์œ„ ์ฝ”๋“œ๋ฅผ ์ฝ”๋“œ ์ตœ์ƒ๋‹จ์— ์ถ”๊ฐ€ํ•œ๋‹ค.

๋Š๋ฆฌ์ง€๋งŒ ์ •ํ™•ํ•˜๊ฒŒ

๋ฌธ์ œ๋ฅผ ์ •ํ™•ํ•˜๊ฒŒ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด ์ดํ•ดํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

  1. CommonJS

    • ๋ธŒ๋ผ์šฐ์ €์—์„œ๋งŒ ๋™์ž‘ํ•˜๋Š” JavaScript๋ฅผ ์„œ๋ฒ„๋‚˜ ๋ฐ์Šคํฌํ†ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋„ ๋™์ž‘ํ•˜๊ฒŒ(์ฆ‰, Commonํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ฒŒ!)๋” ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ JavaScript์˜ ๋ฒ”์šฉ์ ์ธ ์ŠคํŽ™ ์ •์˜
    • ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์™€์„œ ์“ธ ๋•Œ๋Š” require, ๋ชจ๋“ˆ์„ ๋‚ด๋ณด๋‚ผ ๋•Œ๋Š” module.exports ์‚ฌ์šฉ
    • ๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค!
  2. AMD(Asynchronous Module Definition)

    • CommonJS๋Š” ๋ชจ๋“  ํŒŒ์ผ์ด ๋กœ์ปฌ ๋””์Šคํฌ์— ์žˆ์–ด ํ•„์š”ํ•  ๋•Œ ๋ฐ”๋กœ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์„ ์ „์ œ๋กœ ํ•จ (๋™๊ธฐ์ ์ธ ๋™์ž‘ ๊ฐ€๋Šฅ)
    • But, ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” CommonJS์˜ ์ด๋Ÿฐ ๋ฐฉ์‹์ด ํ•„์š”ํ•œ ๋ชจ๋“ˆ์„ ๋ชจ๋‘ ๋‹ค์šด๋กœ๋“œํ•  ๋•Œ๊นŒ์ง€ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์น˜๋ช…์ ์ธ ๋‹จ์ ์œผ๋กœ ๋‹ค๊ฐ€์˜จ๋‹ค!
    • AMD๋Š” ๋น„๋™๊ธฐ ์ƒํ™ฉ์—์„œ๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ์‹œ์Šคํ…œ
    • define์œผ๋กœ ๋ชจ๋“ˆ์„ ์ถ”์ถœํ•˜๊ณ , require๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  3. UMD(Universal Module Definition)

    • CommonJS์™€ AMD๋Š” ์ด๋Ÿฐ ์ฐจ์ด๋ฅผ ๊ฐ–๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋กœ ํ˜ธํ™˜๋˜์ง€ ์•Š์Œ
    • ์ด๋Ÿฌํ•œ ํ˜ธํ™˜์„ฑ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ๊ฒŒ UMD!
    • UMD๋Š” '๋ชจ๋“ˆ ๋กœ๋”๋ฅผ ํ™•์ธํ•˜๋Š” ์ฆ‰์‹œ ์‹คํ–‰ ํ•จ์ˆ˜'์™€ '๋ชจ๋“ˆ์„ ์ƒ์„ฑํ•˜๋Š” ์ต๋ช… ํ•จ์ˆ˜', ์ด๋ ‡๊ฒŒ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋จ
    • UMD ๊ณต์‹ ๋ฌธ์„œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Œ
(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define([], factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root.returnExports = factory();
  }
}(typeof self !== 'undefined' ? self : this, function () {

    // Just return a value to define the module export.
    // This example returns an object, but the module
    // can return a function as the exported value.
    return {};
}));
  1. ESM(ECMAScript Module)

    • ES6์— ์ถ”๊ฐ€๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ๊ธฐ๋Šฅ
    • ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ import, ๋‚ด๋ณด๋‚ผ ๋•Œ export ์‚ฌ์šฉ

์ฆ‰, ๋ฌธ์ œ๋Š” Babel์ด๋‚˜ Webpack ๋“ฑ๋“ฑ๊ณผ ํ”„๋กœ์ ํŠธ์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ๋ชจ๋“ˆ ์‹œ์Šคํ…œ๊ณผ ์„ค์ • ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ๋‹ฌ๋ผ์„œ ์ƒ๊ธฐ๋Š” ๋ฌธ์ œ์˜€๋˜ ๊ฒƒ ๊ฐ™๋‹ค.
codesandbox์—์„œ ๋ฏธ๋ฆฌ ์„ธํŒ…๋œ ๋ณด์ผ๋Ÿฌ ํ”Œ๋ ˆ์ดํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚ด๋ถ€ ์„ธํŒ…๊นŒ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ๋‚ด๊ฐ€ ์ถ”์ธกํ•œ ๋ฐ”๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. ํ”„๋กœ์ ํŠธ์— ์„ค์น˜๋œ ํŒจํ‚ค์ง€ ์ค‘์— react-scripts๊ฐ€ ์žˆ์—ˆ๊ณ , ์ด๋Š” CRA๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ž๋™์œผ๋กœ ํŒจํ‚ค์ง€๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  CRA๋Š” Babel์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ํ”„๋กœ์ ํŠธ์—์„œ๋Š” Webpack ์„ค์ • ๋˜ํ•œ ์„ธํŒ…์ด ๋˜์–ด ์žˆ์–ด์„œ ์ด ๋‘˜์ด ์ถฉ๋Œ์„ ์ผ์œผํ‚ค๋Š” ๊ฒŒ ์•„๋‹๊นŒ ์‹ถ์—ˆ๋‹ค.

import React from 'react'

๊ฒฐ๊ตญ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์€ ์ฝ”๋“œ ์ƒ๋‹จ์— ์œ„ ํ•œ ์ค„์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๊ธฐ ์‹ซ๋‹ค๋ฉด ์ถฉ๋Œ์„ ์ผ์œผํ‚ค๋Š” ์„ค์ •์„ ์ฐพ์•„ ESM์œผ๋กœ ํ†ต์ผ์‹œ์ผœ์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค!

+) ์ฐธ๊ณ 

  • Webpack : ์ฐธ์กฐ ๊ด€๊ณ„์— ์žˆ๋Š” ๋ชจ๋“ˆ๋“ค์„ ํ•˜๋‚˜๋กœ ๋ชจ์•„์ฃผ๋Š” ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ
  • Loader : Webpack์€ ๋ชจ๋“  ํŒŒ์ผ์„ ๋ชจ๋“ˆ๋กœ ๊ด€๋ฆฌํ•˜๋‚˜, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฐ–์— ๋ชจ๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€, ํฐํŠธ ๋“ฑ์˜ ํŒŒ์ผ์„ Webpack์ด ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜

๐Ÿ™‡โ€โ™‚๏ธ ์ฐธ๊ณ ์ž๋ฃŒ

profile
์‰ฝ๊ฒŒ๋งŒ์‚ด์•„๊ฐ€๋ฉด์žฌ๋ฏธ์—†์–ด๋น™๊ณ 
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€