Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js Middleware not working as expected with Node.js native modules #77807

Closed
Eprince-hub opened this issue Apr 3, 2025 · 2 comments
Closed
Labels
Middleware Related to Next.js Middleware.

Comments

@Eprince-hub
Copy link

Eprince-hub commented Apr 3, 2025

Link to the code that reproduces this issue

https://github.com/Eprince-hub/test-middleware

To Reproduce

  1. Clone https://github.com/Eprince-hub/test-middleware: git clone https://github.com/Eprince-hub/test-middleware
  2. Install Dependecnies: pnpm i
  3. Start the development server: pnpm dev

Current vs. Expected behavior

According to this release blog post on the latest Next.js canary version (https://nextjs.org/blog/next-15-2#nodejs-middleware-experimental), the Next.js middleware, AKA middleware.ts, is supposed to work without issues when using Node.js native modules.

So this code below should work but that is not the case.

import path from 'node:path';
console.log('test', path.join(process.cwd()));

The above code produces the error below with a 500 error that breaks the application

 ⨯ node:path
Module build failed: UnhandledSchemeError: Reading from "node:path" 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.
Import trace for requested module:
node:path
 GET /_next/static/webpack/adfdf801ae66cabf.webpack.hot-update.json 500 in 37ms

Provide environment information

Operating System:
Mac Os

Environment:
Node: v22.13.1
pnpm: 10.7.0

Packages
"next": "15.3.0-canary.33"

next.config.ts


import type { NextConfig } from 'next';

const nextConfig: NextConfig = {
  experimental: {
    nodeMiddleware: true,
  },
};

export default nextConfig;

Which area(s) are affected? (Select all that apply)

Middleware

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

Tested with other lower versions of the Next.js canary, but the issue persisted in all cases

@github-actions github-actions bot added the Middleware Related to Next.js Middleware. label Apr 3, 2025
@cshawaus
Copy link

cshawaus commented Apr 4, 2025

As per the blog post, you also need to set the runtime to nodejs for middleware.ts.

export const config = {
  runtime: 'nodejs',
};

@Eprince-hub
Copy link
Author

As per the blog post, you also need to set the runtime to nodejs for middleware.ts.

export const config = {
runtime: 'nodejs',
};

Thank you so much, I must have skipped that. Adding the runtime: 'nodejs', to the middleware config solved it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Middleware Related to Next.js Middleware.
Projects
None yet
Development

No branches or pull requests

2 participants