작성
·
26
0
강사님 안녕하세요, 로그인/회원가입 시 middleware export 되지않는 이슈로 질문남깁니다..!
터미널에서 middleware가 export가 되지않는다고 뜨는데 import하는 방식이 잘못된건지 여쭙니다.
// middleware.ts
import { auth as middleware } from "./auth"
export const config = {
matcher: ['/compose/tweet', '/home', '/explore', '/messages', '/search'],
}
//auth.ts
import NextAuth, {CredentialsSignin} from "next-auth"
import CredentialsProvider from "next-auth/providers/credentials";
export const {
handlers: { GET, POST },
auth,
signIn,
} = NextAuth({
pages: {
signIn: "/i/flow/login",
newUser: "/i/flow/signup",
},
providers: [
CredentialsProvider({
async authorize(credentials) {
const authResponse = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/login`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: credentials.username,
password: credentials.password,
}),
})
if (!authResponse.ok) {
return null
}
const user = await authResponse.json();
return {
email: user.id,
name: user.nickname,
image: user.image,
...user,
};
},
}),
]
});
// package.json
{
"name": "x-com",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@auth/core": "^0.27.0",
"@vanilla-extract/css": "^1.16.1",
"@vanilla-extract/recipes": "^0.5.5",
"@vanilla-extract/sprinkles": "^1.6.3",
"classnames": "^2.5.1",
"clsx": "^2.1.1",
"dayjs": "^1.11.13",
"next": "^15.0.4",
"next-auth": "^5.0.0-beta.25",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"react-textarea-autosize": "^8.5.6",
},
"devDependencies": {
"@faker-js/faker": "^9.3.0",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"@vanilla-extract/next-plugin": "^2.4.7",
"@vanilla-extract/webpack-plugin": "^2.3.15",
"msw": "^2.6.8",
"typescript": "^5"
},
"msw": {
"workerDirectory": [
"public"
]
}
}
<터미널 오류내용>
⨯ Error: The Middleware "/src/middleware" must export a `middleware` or a `default` function
at <unknown> (webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js:12)
at eval (webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js?absolutePagePath=C%3A%5CUsers%5Calsdu%5CDocuments%5CGitHub%5Canimals_forest%5Csrc%5Cmiddleware.ts&page=%2Fsrc%2Fmiddleware&rootDir=C%3A%5CUsers%5Calsdu%5CDocuments%5CGitHub%5Canimals_forest&matchers=&preferredRegion=&middlewareConfig=e30%3D!:12:0)
at (middleware)/./node_modules/next/dist/build/webpack/loaders/next-middleware-loader.js?absolutePagePath=C%3A%5CUsers%5Calsdu%5CDocuments%5CGitHub%5Canimals_forest%5Csrc%5Cmiddleware.ts&page=%2Fsrc%2Fmiddleware&rootDir=C%3A%5CUsers%5Calsdu%5CDocuments%5CGitHub%5Canimals_forest&matchers=&preferredRegion=&middlewareConfig=e30%3D! (.next\server\src\middleware.js:642:1)
at __webpack_require__ (.next\server\edge-runtime-webpack.js:37:33)
at __webpack_exec__ (.next\server\src\middleware.js:681:48)
at <unknown> (.next\server\src\middleware.js:682:37)
at webpackJsonpCallback (.next\server\edge-runtime-webpack.js:1219:39)
at <unknown> (.next\server\src\middleware.js:9:61)
at evaluateInContext (node_modules\next\dist\server\web\sandbox\context.js:420:38)
GET /home 404 in 4ms
Failed to get source map: Error: Failed to read file contents of C:\Users\alsdu\Documents\GitHub\animals_forest\webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js.
at getSourceMapFromFile (node_modules\next\src\client\components\react-dev-overlay\internal\helpers\get-source-map-from-file.ts:20:10)
at async getSource (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:228:22)
at async (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:319:17)
at async HotReloaderWebpack.run (node_modules\next\src\server\dev\hot-reloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-server.ts:576:6)
at async Server.requestListener (node_modules\next\src\server\lib\start-server.ts:146:6) {
[cause]: Error: ENOENT: no such file or directory, open 'C:\Users\alsdu\Documents\GitHub\animals_forest\webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js'
at async getSourceMapFromFile (node_modules\next\src\client\components\react-dev-overlay\internal\helpers\get-source-map-from-file.ts:18:19)
at async getSource (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:228:22)
at async (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:319:17)
at async HotReloaderWebpack.run (node_modules\next\src\server\dev\hot-reloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-s at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-server.ts:576:6)
at async Server.requestListener (node_modules\next\src\server\lib\start-server.ts:146:6) {
[cause]: Error: ENOENT: no such file or directory, open 'C:\Users\alsdu\Documents\GitHub\animals_forest\webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js'
at async getSourceMapFromFile (node_modules\next\src\client\components\react-dev-overlay\internal\helpers\get-source-map-from-file.ts:18:19)
at async getSource (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:228:22)
at async (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:319:17)
at async HotReloaderWebpack.run (node_modules\next\src\server\dev\hot-reloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-sserver.ts:146:6) {
[cause]: Error: ENOENT: no such file or directory, open 'C:\Users\alsdu\Documents\GitHub\animals_forest\webpack-internal:\(middleware)\node_modules\next\dist\build\webpack\loaders\next-middleware-loader.js'
at async getSourceMapFromFile (node_modules\next\src\client\components\react-dev-overlay\internal\helpers\get-source-map-from-file.ts:18:19)
at async getSource (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:228:22)
at async (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:319:17)
at async HotReloaderWebpack.run (node_modules\next\src\server\dev\hot-reloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-soverlay\server\middleware.ts:228:22)
at async (node_modules\next\src\client\components\react-dev-overlay\server\middleware.ts:319:17)
at async HotReloaderWebpack.run (node_modules\next\src\server\dev\hot-reloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-sreloader-webpack.ts:392:6)
at async handleRequest (node_modules\next\src\server\lib\router-server.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-s.ts:319:34)
at async requestHandlerImpl (node_modules\next\src\server\lib\router-server.ts:576:6)
at async Server.requestListener (node_modules\next\src\server\lib\start-server.ts:146:6) {
errno: -4058,
code: 'ENOENT',
syscall: 'open',
path: 'C:\\Users\\alss\\Documents\\GitHub\\x-com\\webpack-internal:\\(middleware)\\node_modules\\next\\dist\\build\\webpack\\loaders\\next-middleware-loader.js'
}
}
답변 2
0
export { auth as middleware } from "./auth"
입니다.
import { auth } from "./auth";
export const middleware = auth;
를 줄인 겁니다.
0
안녕하세요, 인프런 AI 인턴입니다. 인생꿀잼님께서 직면한 문제를 해결하기 위한 힌트를 제공해 드리겠습니다. 질문에서 제공된 오류 메시지는 middleware.ts
파일에서 middleware
나 default
함수를 제대로 export하지 않은 것과 관련이 있습니다.
next-auth
와 관련된 미들웨어 설정에서 이와 같은 오류는 exported
미들웨어가 요구되는 구조와 일치하지 않을 때 많이 나타납니다. 현재 사용하신 코드에서는 auth
객체를 바로 import해서 사용하는 방식이 미들웨어에서 기대하는 export 형태와 맞지 않는 것으로 보입니다.
middleware.ts
파일에서 middleware
를 제대로 export하도록 해주세요.다음은 올바른 형식의 예시입니다:
// middleware.ts
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
// 여기에 요청에 대한 처리를 추가하세요.
return NextResponse.next();
}
export const config = {
matcher: ['/compose/tweet', '/home', '/explore', '/messages', '/search'],
};
Module Import 형식 확인: 현재 auth.ts
에서 export
된 함수나 객체가 정확히 무엇인지 확인하고, 올바르게 가져오고 있는지 확인하는 것이 중요합니다. 만약 변경된 구조가 있다면, documentation을 참고하여 맞는 방식으로 import/export 설정을 변경해주세요.
버전 호환성 확인: next-auth
, next
, Node.js 프레임워크의 버전 호환성이 문제를 일으킬 수 있습니다. 사용 중인 라이브러리의 최신 버전 및 Next.js 버전과의 호환성을 확인해 보세요.
유사한 문제를 겪는 분들에게 도움이 될만한 기존 해결 사례들을 몇 가지 추천드립니다:
저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다. 현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏 추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.