-
SummaryHi. I'm able to use
Additional informationnode version: v18.14.2
nextjs: "latest" ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 17 comments 15 replies
-
Seeing the same when querying db using drizzle. Not a blocking issue for me (was just a convenient spot for me to exercise a test query) but begs the question re: limitation on middleware -- since this is server-side code path it's unclear why fails in middleware where same code block succeeds elsewhere. |
Beta Was this translation helpful? Give feedback.
-
Same issue here
|
Beta Was this translation helpful? Give feedback.
-
For anybody tracking this, I believe the issue is that nextjs middleware does not support node and this may be by design, at least for now -- see https://nextjs.org/docs/messages/node-module-in-edge-runtime |
Beta Was this translation helpful? Give feedback.
-
any update on this issue? I am getting node:crypto error for drizzle as well |
Beta Was this translation helpful? Give feedback.
-
The same error occurred when Next.js was at version 13.4.4, but upgrading to 13.4.7 fixed the issue. |
Beta Was this translation helpful? Give feedback.
-
Any solution to this in place yet? |
Beta Was this translation helpful? Give feedback.
-
I had the same issue, just a few minutes ago. How i resolve d it was use "use server" Drizzle ORM - What im using.
'use server';
import db from '..';
import { blogs, roleBlog } from '../schema';
import { errorRes } from '@/lib/utils/constants';
import { BlogProps, ResObj, err } from '@/interface'; Error: Module build failed: UnhandledSchemeError: Reading from "node:crypto" is not handled by plugins (Unhandled scheme). Webpack supports "data:" and "file:" URIs by default. You may need an additional plugin to handle "node:" URIs. Im assuning you are trying to make either a get or post http in a client components, if so, just put |
Beta Was this translation helpful? Give feedback.
-
I had the same problem recently and adding |
Beta Was this translation helpful? Give feedback.
-
"use server" does not work for me. In my case, I imported another package in middleware.ts and in that package it is using node:crypto. Putting "use server" in middleware.ts and even the imported package does not work. |
Beta Was this translation helpful? Give feedback.
-
same here |
Beta Was this translation helpful? Give feedback.
-
I'm encountering the same problem here, except I don't even have any middleware importing the server-only code. It's only reachable via API route and by instrumentation. Edit: I just learned that the instrumentation hook is being evaluated for client-side use? I unwired the instrumentation hook and the error went away. |
Beta Was this translation helpful? Give feedback.
-
Still not possible to use node:crypto with nextjs ? |
Beta Was this translation helpful? Give feedback.
-
What is the point of restricting nodejs modules inside the middleware ?? |
Beta Was this translation helpful? Give feedback.
-
think of the middleware as client side code. You'll have to do some /api/ route work around and fetch that endpoint in the middleware. Here is my example middleware.ts with the idea of redirecting a route to a blog post from it's id to it's slug btw I'm using Next 14 with app router // cred - https://www.youtube.com/watch?v=xrvul-JrKFI
// other tips - https://borstch.com/blog/development/middleware-usage-in-nextjs-14
import { Post } from '@ks/types';
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(request: NextRequest) {
//todo set user chosen theme here? from cookies
if (request.nextUrl.pathname.startsWith('/blog/id')) {
try {
const url = new URL(request.url)
const postId = url.pathname.split('/blog/id/')[1]
const res = await fetch( url.origin + `/api/gql/noauth`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query Post($where: PostWhereUniqueInput!) {
post(where: $where) {
slug
}
}
`,
variables: {
where: {
id: postId
}
}
}),
})
const {post} = await res.json() as {post: Post}
if(!post.slug) return console.log('no post:slug found');
return NextResponse.redirect(new URL(`/blog/${post?.slug}`, request.url))
} catch (error) {
return new Response('Error processing request', { status: 500 });
}
}
}
// See "Matching Paths" https://nextjs.org/docs/app/building-your-application/routing/middleware#matching-paths
export const config = {
matcher:[ '/blog/id/:id*'],
} |
Beta Was this translation helpful? Give feedback.
-
Any solution for this? |
Beta Was this translation helpful? Give feedback.
-
Jumping in here to say that I think there are good design reasons to design middleware for the Node environment. I think the correct tack is to make a Route Handler with your Node-environment libraries like node:crypto, and call that route handler from your middleware. |
Beta Was this translation helpful? Give feedback.
-
Good news everyone, Node.js Middleware support landed in Next.js 15.2 (experimental). This is now ready for testing. For any issues you run into, please open new issues so we can track and address before marking it as stable. Thank you! https://nextjs.org/blog/next-15-2#nodejs-middleware-experimental |
Beta Was this translation helpful? Give feedback.
Good news everyone, Node.js Middleware support landed in Next.js 15.2 (experimental). This is now ready for testing. For any issues you run into, please open new issues so we can track and address before marking it as stable. Thank you!
https://nextjs.org/blog/next-15-2#nodejs-middleware-experimental