eungyeole / isomorphic-next

A "single implementation" for both server and client in Next.js is possible.
7 stars 2 forks source link

Package cannot find next/headers when deployed to production #5

Open SerenModz21 opened 2 weeks ago

SerenModz21 commented 2 weeks ago

When I deploy my Next.js application to production, isomorphic-next fails to find next/headers:

Error: Cannot find module 'next/headers'
Require stack:
- /app/.next/server/chunks/349.js
- /app/.next/server/webpack-runtime.js
- /app/.next/server/pages/_document.js
- /app/node_modules/next/dist/server/require.js
- /app/node_modules/next/dist/server/next-server.js
- /app/node_modules/next/dist/server/next.js
- /app/server.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at /app/node_modules/next/dist/server/require-hook.js:55:36
    at Module._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at mod.require (/app/node_modules/next/dist/server/require-hook.js:65:28)
    at require (node:internal/modules/helpers:179:18)
    at o (/app/.next/server/chunks/349.js:2:19063)
    at u.get (/app/.next/server/chunks/349.js:2:19153)
    at l (/app/.next/server/chunks/223.js:1:22640)
    at a (/app/.next/server/app/dashboard/page.js:1:33428) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/.next/server/chunks/349.js',
    '/app/.next/server/webpack-runtime.js',
    '/app/.next/server/pages/_document.js',
    '/app/node_modules/next/dist/server/require.js',
    '/app/node_modules/next/dist/server/next-server.js',
    '/app/node_modules/next/dist/server/next.js',
    '/app/server.js'
  ]
}

Versions: next 14.2.13 isomorphic-next 0.0.1-Prerelease

I'm specifically stating that it isomorphic-next failing to find it because I use the cookies function from next/headers in parts of my codebase, to check if a cookie exists. That code works fine but when isomorphic-next tries to require it, it fails to find it.

The only thing I can think of is that Next.js modifies next/headers imports at build time.

If it's worth noting, I deploy my application using Docker and with a standalone build.

eungyeole commented 2 weeks ago

Did you use pages router?

SerenModz21 commented 2 weeks ago

No, I use the app router.

eungyeole commented 2 weeks ago

Can you provide repo or example?

SerenModz21 commented 2 weeks ago

Unfortunately, the repo for the project that I work on is private. However, I will make a new repo to hopefully reproduce it.

SerenModz21 commented 2 weeks ago

Here is a repo that reproduces the issue: https://github.com/SerenModz21/isomorphic-next-reproduction

SerenModz21 commented 2 weeks ago

By the way, not that this is to do with this issue, your typecasting for getAll is completely changing the actual return type. You're telling typescript to return a Record but it's an object array. For example: { name: string; value: string }[]

This can lead to confusion when people try to use Object.entries() for what they'll believe is an object, when it's an array already. It happened to me and had to typecast it back to its correct object array.

eungyeole commented 2 weeks ago

I'll also check that issue.

SerenModz21 commented 2 weeks ago

After having a proper look, I found that cookie.parse() returns an object (as per the string record type currently), while Next's cookies().getAll() is an array of objects as mentioned in my previous comment.

To resolve the type conflict, you could either have it return a union type or format one of the outputs to match the other.

eungyeole commented 2 weeks ago

type definition issue has been resolved. Please check version 0.0.2.

eungyeole commented 2 weeks ago

We are currently investigating the issue: Error: Cannot find module 'next/headers'.