[issues] Package path ./browser is not exported from package [next.js]
์นดํ
๊ณ ๋ฆฌ : Issues
ํ๊ทธ: issue · Next.js · next 14.0.3 · MSW · browser.ts · handlers.ts · http.ts · issues · Package path ./browser is not exported from package · ์๋ฌ
ํ๊ทธ: issue · Next.js · next 14.0.3 · MSW · browser.ts · handlers.ts · http.ts · issues · Package path ./browser is not exported from package · ์๋ฌ
๋ฒ์
Browser : Chrome
MSW: 2.0.9
Node.js : 20.10.0
Next.js: 14.0.3
์๋ฌ ์ฝ๋
//broswer.ts
"use client";
import { setupWorker } from "msw/browser";
import { handlers } from "./handlers";
export const worker = setupWorker(...handlers);
๋ฐ์ ์์ธ
https://github.com/mswjs/msw/issues/1877
Next 14 ๋ฒ์ ์์ MSW ๋ชจํน ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋ Service mock enabled๊ฐ ๋ํ๋์ง ์๊ณ Package path ./browser is not exported from package
๊ฐ ๋ํ๋๋ ํ์
ํด๊ฒฐ ๋ฐฉ๋ฒ
๊ทผ๋ณธ์ ์ธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์๋๋ค. MSW ์ ๋ฐ์ดํธ๊น์ง ์์๋ก Node.js์ ์๋ฒ๋ฅผ ๊ฐ๋ ์ํค๋ ๋ฐฉ๋ฒ
//... mock/browser.ts
import { setupWorker } from "msw/browser";
import { handlers } from "./handlers";
export const worker = setupWorker(...handlers);
// ... mock/handlers.ts
import { http, HttpResponse } from "msw";
import order from "./MOCK_DATA.json";
let orders = order;
/* msw ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ์ฌ mock API ๊ตฌํ */
export const handlers = [
/* ์ฃผ๋ฌธ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ */
http.get("/orders", () => {
return HttpResponse.json(orders);
}),
];
// ... mock/http.ts
import { handlers } from "./handlers";
import express from "express";
import cors from "cors";
import { createMiddleware } from "@mswjs/http-middleware";
const app = express();
const port = 8080;
app.use(
cors({
origin: "http//localhost:8080",
optionsSuccessStatus: 200,
credentials: true,
})
);
app.use(express.json());
app.use(createMiddleware(...handlers));
app.listen(port, () => console.log(`Mock Server is running,${port}`));
//package.json
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"mock": "npx tsx watch ./src/app/mock/http.ts"
},
๋ฐ๋ก ํฐ๋ฏธ๋์ ์ด์ด ์์๋ก mock ์๋ฒ๋ฅผ ๊ฐ๋
์ฐธ์กฐ : https://github.com/ZeroCho/next-app-router-z