Next.js Discord

Discord Forum

Observed difference between `export const config = ...` and `const config = ...; export { config };`

Unanswered
Gharial posted this in #help-forum
Open in Discord
GharialOP
From what I have read about module specification ([1](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export) [2](https://exploringjs.com/es6/ch_modules.html#sec_importing-exporting-details) [3](https://262.ecma-international.org/6.0/#sec-exports)) it appears that these two forms should perform the same (both forms below represent snippets from /src/middleware.ts in my app):

"form 1":
export const config = { ... };

vs "form 2":
const config = { ... };
export { config };


(in addition to a config export, I'm exporting middleware but the behavioral differences I'm seeing in my app don't seem to be sensitive to my choice how to export middleware).

My [foolish consistency](https://en.wikipedia.org/wiki/Self-Reliance) causes me to prefer "form 2", but when I do that, my middleware (which I use to enforce authentication for most paths) gets stuck in a redirect loop; whereas, "form 1" works as I expect (the config includes a matcher that is used to prevent /login requests being redirected to /login).

My question for this forum is: is there some nuance of ESM that I've missed noticing, or is there something about how Next.js processes the middleware.ts module that causes the observed difference in behavior between "form 1" and "form 2"? That is, is my problem with my understanding of ESM (both forms should have the same result) or with Next.js? Or, something else altogether?

package.json:
{
  ...
  "dependencies": {
    ...,
    "next": "^15.2.4",
  },
  "devDependencies": {
    ...,
    "@types/node": "^20.17.27",
    ...,
    "typescript": "^5.8.2",
  }
  ...
}

(I don't know whether those coordinates bear on my question, but they're the only ones that I suspect might; I'm glad to edit to add anything else that may help me gain understanding of this issue).

1 Reply

GharialOP
FWIW, I have been able to preserve my preferred export style AND matcher behavior, by performing the path match early in the middleware function. Still, I'm curious to learn why non-inlined export declaration doesn't work for that const config.