Skip to content
This repository has been archived by the owner on Oct 30, 2024. It is now read-only.

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

Open
SerenModz21 opened this issue Sep 29, 2024 · 10 comments
Open

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

SerenModz21 opened this issue Sep 29, 2024 · 10 comments

Comments

@SerenModz21
Copy link

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
Copy link
Owner

Did you use pages router?

@SerenModz21
Copy link
Author

No, I use the app router.

@eungyeole
Copy link
Owner

eungyeole commented Sep 29, 2024

Can you provide repo or example?

@SerenModz21
Copy link
Author

SerenModz21 commented Sep 29, 2024

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

@SerenModz21
Copy link
Author

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

@SerenModz21
Copy link
Author

SerenModz21 commented Sep 29, 2024

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
Copy link
Owner

eungyeole commented Oct 2, 2024

I'll also check that issue.

@SerenModz21
Copy link
Author

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
Copy link
Owner

eungyeole commented Oct 2, 2024

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

@eungyeole
Copy link
Owner

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants