question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Edge compiler is not choosing browser bundle for module

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Enterprise
Binaries:
  Node: 16.14.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 12.1.6
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

next dev

Describe the Bug

We utilize the debug module in our codebase and are hoping to be able to within edge middleware as well. My understanding is that the edge compiler will favor a browser bundle over the main / node.js bundle if present. See https://github.com/vercel/next.js/issues/32369#issuecomment-1092859859.

However, this doesn’t seem to be the case when attempting to use the debug module. browser is defined, yet when running with next dev we see the node.js bundle is being used.

error - ./node_modules/debug/src/node.js:5:0
Module not found: Can't resolve 'tty'

Import trace for requested module:
./node_modules/debug/src/index.js
./pages/_middleware.ts

https://nextjs.org/docs/messages/module-not-found

You're using a Node.js module (tty) which is not supported in the Edge Runtime.
Learn more: https://nextjs.org/docs/api-reference/edge-runtime

Expected Behavior

I would expect the browser bundle is used by the edge compiler.

To Reproduce

  1. Run:
npx create-next-app@latest --ts
npm i debug
npm i @types/debug -D
  1. Create _pages/middleware.ts:
import type { NextFetchEvent, NextRequest } from 'next/server';
import debug from 'debug';

export function middleware(req: NextRequest, ev: NextFetchEvent) {
  debug('Hellow, world!');
  return new Response('Hello, world!');
}
  1. Run: npm run dev

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
huozhicommented, May 12, 2022

@ambrauer Yeah that alias solution also works. And thanks for the feedback, we’ll try to improve the documentation on our side

1reaction
ambrauercommented, May 12, 2022

Thanks for the clarification @huozhi. It’s not as straightforward for us since debug is used in a shared package (used on all runtimes). We will need to re-evaluate, but for now we have a workaround using a webpack config adjustment.

webpack: (config, options) => {
  if (options.isServer && options.nextRuntime === 'edge') {
	config.resolve.alias.debug = require.resolve('debug/src/browser');
  }

  return config;
}

Between this change and removal of shims for native Node modules (in #36776 mentioned by @wadehammes), just trying to recover from the breaking changes dropped for edge middleware in 12.1.6. All totally expected being beta and all, just caught us by surprise 😃

Thanks again!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Compiling and bundling TypeScript libraries with Webpack
Since I started working on UI-Router React I've been enjoying writing TypeScript. There are both advantages and disadvantages working with a typed and ......
Read more >
Publishing Node modules with TypeScript and ES modules
Learn how you can write your packages in TypeScript but still ship your users modern JavaScript code that they won't have to manually ......
Read more >
Could not find a declaration file for module 'module-name ...
In order for this to work, I had to make declare module '...' the first line of code in the module.d.ts file, and...
Read more >
Bundling and Minification | Microsoft Learn
Debugging Bundled and Minified JavaScript · Select the bundle containing the JavaScript function you want to debug using the assets button.
Read more >
Advanced Features: Next.js Compiler
They will not be available once the minifier is made stable. // next.config.js module.exports = { experimental: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found