[Next.js] 01. ์์ํ๊ธฐ
๐ก 1. next js ์์ ์ ์ธํ
๐ฃ Node.js ๋ฒ์ 10.13 ์ด์ ์ค์น
Node.js ์ค์น ํ ์ค์น๊ฐ ์ ์์ ์ผ๋ก ๋์๋์ง ํ์ธํ๊ธฐ ์ํด์ ํฐ๋ฏธ๋์ ์ฌ์ ํ 'node -v'๋ฅผ ์ ๋ ฅํ์ฌ ํ์ธ.
//๋
ธ๋ ๋ฒ์ ํ์ธ
node -v
๐ฃ npm ํน์ yarn
npm (Node Package manager)์ ๋ ธ๋ ์ค์น ์ ์๋์ ์ผ๋ก ๊ฐ์ด ์ค์น๊ฐ ๋ฉ๋๋ค.
// npm ๋ฒ์ ํ์ธ
npm -v
ํฐ๋ฏธ๋์์ 'npm -v'๋ฅผ ์ ๋ ฅํ๋ฉด ์ค์น ๋ฒ์ ์ด ๋์ต๋๋ค.
yarn์ ํ์ด์ค๋ถ์์ ๋ง๋ ์๋ฐ์คํฌ๋ฆฝํธ ํจํค์ง ๋งค๋์ ์ ๋๋ค. npm๊ณผ ๊ธฐ๋ฅ์ ๋์ผํ๊ณ , ํ์ด์ค๋ถ ์ธก์์ npm๋ณด๋ค ๋ณด์์ด ๋ฐ์ด๋๊ณ ์ฑ๋ฅ์ด ๋น ๋ฅด๋ค๊ณ ์ฃผ์ฅ์ ํ๊ณ ์์ต๋๋ค ใ ใ .. (์ฌ์ค npm, yarn ๋ ๋ค ์ข์ต๋๋ค.)
yarn ์ค์น ๋ฐฉ๋ฒ์ ํฐ๋ฏธ๋์์ npm install yarn -g๋ผ๊ณ ์ ๋ ฅํ๋ฉด ๋ฉ๋๋ค.
// yarn ์ค์น
npm install yarn -g
๐ก 2. Next.js ์์ํ๊ธฐ
๐ฃ 2.1 CNA์ ์ด์ฉํ์ฌ ์ค์น
npx create-next-app
or
yarn create-next-app
// typescript ๋ฒ์
npx create-next-app --typescript
or
yarn create next-app --typescript
ํฐ๋ฏธ๋์ npx create-next-app์ด๋ผ๊ณ ์ ๋ ฅํ๋ฉด ์๋์ ๊ฐ์ด ํ๋ก์ ํธ ๋ช ์ ์ ๋ ฅํ๋ฉด, ์๋์ผ๋ก ์ค์น๊ฐ ์งํ๋ฉ๋๋ค.
npx create-next-app
Need to install the following packages:
create-next-app
Ok to proceed? (y) y
√ What is your project named? ... my-first-next-app
Creating a new Next.js app in D:\GIT\my-first-next-app.
Using npm.
Installing dependencies:
- react
- react-dom
- next
added 16 packages, and audited 17 packages in 10s
2 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Installing devDependencies:
- eslint
- eslint-config-next
added 203 packages, and audited 220 packages in 31s
63 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
Initialized a git repository.
Success! Created my-first-next-app at D:\GIT\my-first-next-app
Inside that directory, you can run several commands:
npm run dev
Starts the development server.
npm run build
Builds the app for production.
npm start
Runs the built app in production mode.
We suggest that you begin by typing:
cd my-first-next-app
npm run dev
๐ฃ 2.2 ์์ฑ๋ Next.js ํ์ธํ๊ธฐ
- node_modules ํด๋ : ํ ํ๋ก์ ํธ์ ํ์ํ ๋ ธ๋ ๋ชจ๋๋ค์ ๋ชจ์ ๋์ ํด๋
- pages ํด๋ : ์น์ฌ์ดํธ์ ํ์ด์ง๋ค์ ํ์ผ์ ๋ชจ์ ๋์ ํด๋
- public ํด๋ : next.js ํ๋ก์ ํธ์์ ์ ์ญ์ ์ผ๋ก ์ฌ์ฉํ๋ ์ฌ์ง, ๋์์๋ค์ ๋ชจ์ ๋์ ํด๋
- styles ํด๋ : css๋ค์ ๋ชจ์ ๋์ ํด๋
- .gitignore : ๊นํ๋ธ์ ์ฌ๋ฆด ๋ ์ ์ธํ ํญ๋ชฉ์ ์ ์ด ๋๋ ํ์ผ
- next.config.js : ์นํฉ ๋ฒ๋ค๋ง ๋ฐ api ํต์ ์ ํ ๋ ์ฌ์ฉ
- package.json : ํ ํ๋ก์ ํธ์ ์ค์ ๊ณผ ์ ๋ณด๋ฅผ ์ ์ด ๋์ ํ์ผ
๐ฃ 2.3 Next.js ์คํ
1. ์๋ํฐ(์ ๊ฐ์ ๊ฒฝ์ฐ๋ vscode)์์ ํฐ๋ฏธ๋์ ์ฌ์ ํ 'npm run dev'๋ผ๊ณ ์ ๋ ฅํ๋ฉด next.js๊ฐ ์คํ์ด ๋ฉ๋๋ค.
npm run dev
or
yarn dev
2. ํฌ๋กฌ์ ์ฌ์ ํ ์ฃผ์์ฐฝ์ localhost:3000์ด๋ผ๊ณ ์ ๋ ฅ
๐ฃ 2.4 next.js ์ข ๋ฃํ๊ธฐ
- ์๋ํฐ ํฐ๋ฏธ๋ ํด๋ฆญ ํ ctrl + c ์ ๋ ฅ, ์๋ ์ฌ์ง๊ณผ ๊ฐ์ด ๋ฌธ๊ตฌ๊ฐ ๋จ๋ฉด Y ์ ๋ ฅ ํ ์ํฐ
๐ ์ ๋ฆฌ
- node.js ์ค์นํ๊ธฐ (10.13 ์ด์ ๋ฒ์ ์ผ๋ก)
- npx create-next-app or yarn create-next-app์ผ๋ก ์ค์น
- npm run dev or yarn dev๋ก ์คํ
๊ธด ๊ธ ์ฝ์ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค :)
ํ๋ฆฐ ๋ด์ฉ์ด ์๊ฑฐ๋, ๋ง๋ถ์ผ ๋ด์ฉ์ด ์๋ค๋ฉด ์ธ์ ๋ ์ง ๋๊ธ ๋ฌ์์ฃผ์ธ์!
์ ๊ธ์ด ์กฐ๊ธ์ด๋๋ง ์ฝ์ผ์ ๋ถ๋ค์๊ฒ ๋์์ด ๋๋๋ก ๋
ธ๋ ฅํ๊ฒ ์ต๋๋ค
๋ค์ ํธ์ ๋ดฌ์~