Skip to content

Conversation

@deltaepsilon
Copy link
Contributor

@deltaepsilon deltaepsilon commented May 22, 2023

Summary

DragonZSG discovered a problem with middleware.ts and instrumentation.ts on this Discord thread

I was able to reproduce and found this error:

An error occurred while loading instrumentation hook: (0 , _highlight_run_next__WEBPACK_IMPORTED_MODULE_1__.registerHighlight) is not a function

I was able to resolve the issue with a dynamic import in instrumentation.ts, because it appeared that middleware.ts was attempting to import Node.js-only code in a non-Node.js environment.

I've updated the docs.

image

How did you test this change?

I created a middleware.ts file at the root of e2e/nextjs and reproduced the error.

Subsequent changes to instrumentation.ts resolved the issue

Are there any deployment considerations?

Nope

@deltaepsilon deltaepsilon self-assigned this May 22, 2023
@deltaepsilon deltaepsilon marked this pull request as ready for review May 22, 2023 16:05
@MichaelCGrant
Copy link
Contributor

MichaelCGrant commented May 22, 2023

I think having a middleware.ts file will also cause "__dirname" to not be defined since it is also a nodejs feature.
My solution was:

// next.config.mjs

import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// ...rest of config

@deltaepsilon
Copy link
Contributor Author

I think having a middleware.ts file will also cause "__dirname" to not be defined since it is also a nodejs feature. My solution was:

// next.config.mjs

import { fileURLToPath } from "url";
import { dirname } from "path";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// ...rest of config

That's really interesting. I am often baffled by the Next runtime.

@deltaepsilon deltaepsilon enabled auto-merge (squash) May 22, 2023 18:07
@deltaepsilon
Copy link
Contributor Author

deltaepsilon commented May 22, 2023

image

@deltaepsilon deltaepsilon merged commit 857e91b into main May 22, 2023
@deltaepsilon deltaepsilon deleted the ce/middleware branch May 22, 2023 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants