๐Ÿ“ฆ Node.js ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๋ฌถ๊ธฐ

3930 ๋‹จ์–ด nodetutorialprogrammingjavascript
๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ JavaScript ๋Ÿฐํƒ€์ž„์ธ Node.js ๐Ÿข๋Š” ๋ฌด์—‡๋ณด๋‹ค๋„ ํŒŒ์ผ ์‹œ์Šคํ…œ ์•ก์„ธ์Šค์— ๋Œ€ํ•œ ํƒ์›”ํ•œ ์ง€์›์„ ์ œ๊ณตํ•˜์—ฌ ๋ฌดํ•œํ•œ ๊ฐ€๋Šฅ์„ฑ์˜ ๋ฌธ์„ ์—ด์–ด์ค๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋‚˜ Node.js๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํŒจํ‚ค์ง•ํ•  ์ˆ˜ ์žˆ์–ด ์ „๋‹ฌํ•ด์•ผ ํ•˜๋Š” ํ•ญ๋ชฉ์˜ ๋ฐฐํฌ ๋ฐ ๊ด€๋ฆฌ๋ฅผ ๋‹จ์ˆœํ™”ํ•˜๋Š” ๊ฒฝ์šฐ ์ข…์ข… ๋‹ค๋ฅธ ๋Ÿฐํƒ€์ž„/์–ธ์–ด์— ๋’ค์ฒ˜์ง‘๋‹ˆ๋‹ค.

์ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ตฌ์„ฑ ์š”์†Œ/์ ‘๊ทผ ๋ฐฉ์‹์ด ์žˆ์ง€๋งŒ ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ Node.js๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ ์žฅ์• ๋ฌผ๋กœ ๋ณด์ด์ง€ ์•Š๋„๋ก ๋” ์ž˜ ๋ฌธ์„œํ™”ํ•˜๊ณ  ์ „ํŒŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ Node.js๊ฐ€ ์†”๋ฃจ์…˜ ๊ตฌ์ถ•์— ์‚ฌ์šฉ๋˜๋Š” ์œ„์น˜/์‹œ๊ธฐ์˜ ํ™•์žฅ์„ ์ง€์›ํ•˜๋Š” ๋ฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

์ด ๊ธฐ์‚ฌ๋Š” Node.js ์ƒํƒœ๊ณ„์˜ ๋‘ ๊ฐ€์ง€ ์ฃผ์š” ๊ด€์‹ฌ์‚ฌ์ธ ๋ฒˆ๋“ค๋ง๊ณผ ํŒจํ‚ค์ง•์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค. ๊ทธ๋“ค์— ๋Œ€ํ•ด ๊ฐ„๋‹จํžˆ ์ด์•ผ๊ธฐํ•ฉ์‹œ๋‹ค.

๋ฒˆ๋“ค๋ง์€ ์ฝ”๋“œ์™€ ๋ชจ๋“  ์ข…์†์„ฑ์„ ๋‹จ์ผ ํŒŒ์ผ๋กœ ๋ณ‘ํ•ฉํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ESM ํŒจํ‚ค์ง• ํ˜•์‹์„ ์‚ฌ์šฉํ•˜๋ฉด CJS๋ณด๋‹ค ํ•œ ๊ฐ€์ง€ ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ ํŠธ๋ฆฌ ์‰์ดํ‚น์ž…๋‹ˆ๋‹ค. ํŠธ๋ฆฌ ์‰์ดํ‚น์€ ์ข…์†์„ฑ์—์„œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฝ”๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ๋„๊ตฌ: esbuild , parcel , webpack , rollup , terser .

Node.js ํŒจํ‚ค์ง•์€ ์†Œ์Šค ์ฝ”๋“œ์™€ Node.js ๋Ÿฐํƒ€์ž„์„ ํฌํ•จํ•˜๋Š” ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ๋‹จ์ผ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์ตœ์ข… ์‚ฌ์šฉ์ž์˜ ์ปดํ“จํ„ฐ์— Node.js๋ฅผ ์„ค์น˜ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

์ด ๊ณผ์ •์—์„œ ๋„๊ตฌ๋Š” ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๊ตฌ๋ฌธ ๋ถ„์„ํ•˜๊ณ  require() ์— ๋Œ€ํ•œ ํ˜ธ์ถœ์„ ๊ฐ์ง€ํ•˜๊ณ  ์ข…์†์„ฑ์„ ํŠธ๋ž˜๋ฒ„์Šคํ•˜๊ณ  ์‹คํ–‰ ํŒŒ์ผ์— ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์†Œ์Šค ์ฝ”๋“œ๋Š” V8 ์—”์ง„์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”์ดํŠธ์ฝ”๋“œ๋กœ ์ปดํŒŒ์ผ๋ฉ๋‹ˆ๋‹ค. ๋„๊ตฌ: pkg , ncc , nexe .

๋ฒˆ๋“ค๋กœ esbuild


  • ๋งค์šฐ ๋น ๋ฅธ JavaScript ๋ฐ CSS ๋ฒˆ๋“ค๋Ÿฌ ๋ฐ ์ถ•์†Œ๊ธฐ
  • ๊ฐ€์žฅ ํŽธ๋ฆฌํ•œ
  • ๊ฐ€์žฅ ๋น ๋ฅธ ๋น„๊ต
  • TypeScript ๊ตฌ๋ฌธ, ESM ๋ฐ CJS ์ง€์›
  • ESM์— ๋Œ€ํ•œ ํŠธ๋ฆฌ ์‰์ดํ‚น ์ง€์›
  • ์ถ•์†Œ ๋ฐ ์†Œ์Šค ๋งต ์ง€์›

  • # Output CommonJS bundle
    $ npx esbuild index.js  --bundle --outfile=build.cjs \
    --format=cjs --platform=node
    



    # Output ESM bundle
    # Note that, you may not need the --banner flag.
    # But, in some cases, require() and __dirname are needed.
    $ npx esbuild index.js  --bundle --outfile=build.mjs \
    --format=esm --platform=node --banner:js="
    import {createRequire} from 'module';
    const require = createRequire(import.meta.url);
    import { dirname } from 'path';
    import { fileURLToPath } from 'url';
    const __dirname = dirname(fileURLToPath(import.meta.url));"
    


    ํŒจํ‚ค์ง€์—์„œ ํŒจํ‚ค์ง€๋กœ


  • Node.js ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ ํŒŒ์ผ๋กœ ํŒจํ‚ค์ง•ํ•ฉ๋‹ˆ๋‹ค
  • .
  • Windows, Mac, Linux ๋“ฑ์„ ์œ„ํ•œ ์‹คํ–‰ ํŒŒ์ผ์„ ์ฆ‰์‹œ ๋งŒ๋“ญ๋‹ˆ๋‹ค
  • .
  • Node.js ๋˜๋Š” ์ˆ˜๋ฐฑ ๊ฐœ์˜ ์ข…์†์„ฑ์„ ์„ค์น˜ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

  • # Packaging tools work best with CJS. 
    # These tools don't go well with ESM.
    
    # To package into executable, just take the file outputted
    # by `esbuild`, and pass it to `pkg`, and we're done!
    $ npx pkg build.cjs
    


    ์ด ๋ช…๋ น์€ 3๊ฐœ์˜ ๋ฐ”์ด๋„ˆ๋ฆฌ ์‹คํ–‰ ๊ฐ€๋Šฅ ํŒŒ์ผ build-linux , build-macos ๋ฐ build-win.exe ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ํ”Œ๋žซํผ์— ๋งž๋Š” ์‹คํ–‰ ํŒŒ์ผ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ์ด ํŒŒ์ผ์„ ์ตœ์ข… ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐฐํฌํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Node.js๋‚˜ ์ข…์†์„ฑ ๋“ฑ์„ ์„ค์น˜ํ•˜์ง€ ์•Š๊ณ  ์ด ํŒŒ์ผ ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค!


    ์ฝ์–ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ํฅ๋ฏธ๋กœ์› ๋‚˜์š”? โค๏ธ ๋˜๋Š” ๐Ÿฆ„๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”! ์ œ๊ฐ€ ๋‹ค๋ฃจ๊ณ  ์‹ถ์€ ์ฃผ์ œ๊ฐ€ ์žˆ๋‚˜์š”? ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์„ธ์š”.

    ์ข‹์€ ํ•˜๋ฃจ ๋˜์„ธ์š”!

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ