[issues] Package path ./browser is not exported from package [next.js]


๋ฒ„์ „

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);

image.png

๋ฐœ์ƒ ์›์ธ

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