Cannot find module 'next/dist/server/next-server.js' #39432
-
Hi I'm using vercel to for my mono repo (nx) next app. it is totally okay in my local machine.
Sometime the next-gh page pipeline fails with the following error after the hole build process finished
There are two thing I can't understand:
Then I come to second issue, the installation of new next version take so much time on M1 chips, it is quite fast in linux distributions but take nearly 20 min for me to update each new version. |
Beta Was this translation helpful? Give feedback.
Replies: 34 comments 55 replies
-
@realamirhe same error using Next.js v12.2.1, which version you reverted to in "changing the next version will solve the issue and pass the pipeline again." ? Edit: upgrading to v12.2.5 fixed this for me |
Beta Was this translation helpful? Give feedback.
-
I have the same error with next 12.3.0, deploying on Vercel with Turborepo. |
Beta Was this translation helpful? Give feedback.
-
For me this was caused by yarn workspaces hoisting the node_modules to the root folder, but the nextjs "workspaces": {
"nohoist": [
"**/frontend",
"**/frontend/**"
],
"packages": [
"api",
"frontend"
]
}, This tells yarn to install the frontend/next modules in the frontend folder directly. |
Beta Was this translation helpful? Give feedback.
-
I am experiencing the same with |
Beta Was this translation helpful? Give feedback.
-
Seeing this with Next.js |
Beta Was this translation helpful? Give feedback.
-
Doing redeploy from the deploy page without the cache solves the issue for me. It works on the second deploy of the failing project, but it get's annoying having to manually redeploy after every git push. I think it has something to do with building the cache locally |
Beta Was this translation helpful? Give feedback.
-
I solved it by doing some "fancy" copying and hoisting of the node_modules folder. Here is my Dockerfile
|
Beta Was this translation helpful? Give feedback.
-
I'm seeing this issue with next |
Beta Was this translation helpful? Give feedback.
-
Using next@12.2.6 and turbo@1.6.1. Getting the same error when deploying app to Vercel, fixed by redeploying in Vercel. I think it may have something to do with caching. |
Beta Was this translation helpful? Give feedback.
-
Not sure if this is 100% the same issue, but I'm including this here cause it was pretty infuriating for me to sort this out. So for my builds the issues was actually a docker COPY context problem. So what was happening is that (on my machine) I would run a next build inside my app (a yarn workspace) and it would do a full build and create the
To fix this I needed to make sure that docker would not use that Then my builds started to get consistent. This is the spec I was using for the ignore that allows a separate dockerignore file in each workspace. |
Beta Was this translation helpful? Give feedback.
-
Downgrading tubro to 1.6.2 fixed it for me. |
Beta Was this translation helpful? Give feedback.
-
Same issue with next 13.0.3-canary.4, doing the redeployment without cache solves it. |
Beta Was this translation helpful? Give feedback.
-
For me this issue was because the .dockerignore file contained the following
which resulted in the standalone/.next folder not being copied to my docker image
|
Beta Was this translation helpful? Give feedback.
-
I am experiencing the same issue with the following when setting up
In the end I found this on next doc and applied following on
|
Beta Was this translation helpful? Give feedback.
-
having the same issue with turborepo |
Beta Was this translation helpful? Give feedback.
-
Hi all, I hope to have some additional info for those that run into this error. We talked to our aws enterprise support and they came up with this working example based on the "outputFileTracingRoot" solution: Which you can fork and import via the AWS Management console (Amplify -> Host new App -> Connect to GitHub -> Select Branch -> add "apps/docs" in the monorepo repo field). They also said that the amplify team has acknowledged this problem and is looking into fixing it, but couldn't give an ETA. We use the CDK, so I wrote a project with a CDK project that will create an infra as code pipeline that creates a cloudformation stack with the amplify app in it (configured to use Next.js 12+). See the readme.md for instructions on how to run it. |
Beta Was this translation helpful? Give feedback.
-
Here's a horrible hack, but it could help if you're desperate. The problem for me was output: 'standalone',
experimental: {
outputFileTracingRoot: path.join(__dirname, '../../'),
}, Then I run // shimVercelNodeModules.js
const fs = require('fs')
const path = require('path')
function collectFuncDirectories(dir, funcDirectories = []) {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file)
if (fs.statSync(filePath).isDirectory() && file.endsWith('.func')) {
funcDirectories.push(filePath)
} else if (fs.statSync(filePath).isDirectory()) {
collectFuncDirectories(filePath, funcDirectories)
}
})
return funcDirectories
}
const run = () => {
const foldersToAddNodeModulesTo = collectFuncDirectories(
path.join(__dirname, '.vercel', 'output', 'functions'),
)
console.log(
`Copying node modules for ${foldersToAddNodeModulesTo.length} folders...`,
)
foldersToAddNodeModulesTo.forEach((x) => {
const writePath = path.join(x, 'node_modules')
fs.cpSync(
path.join(__dirname, '.next', 'standalone', 'node_modules'),
writePath,
{
recursive: true,
},
(err) => {
console.log(err)
},
)
})
console.log(`Completed.`)
}
run() |
Beta Was this translation helpful? Give feedback.
-
To extend on my previous comment. We use docker images for our production runtime environments. We're also using a monorepo based upon yarn workspaces. Yarn berry (> 2.x), uses a
Then for our next config we use:
The nextjs compiler web pack then correctly outputs all of the dependencies correctly into the output build.. e.g. So our multi-stage docker build looks like this:
|
Beta Was this translation helpful? Give feedback.
-
Hi, we have identified an issue with turbo/nx cache and have deployed a fix for this, please redeploy your project and give it a try! |
Beta Was this translation helpful? Give feedback.
-
I'm having the same issue too. My setup is the following:
2023-02-16T14:52:40.429+00:00 | Error: Cannot find module 'next/dist/server/next-server'
-- | --
| 2023-02-16T14:52:40.429+00:00 | Require stack:
| 2023-02-16T14:52:40.429+00:00 | - /tmp/app/server.js
| 2023-02-16T14:52:40.429+00:00 | at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
| 2023-02-16T14:52:40.429+00:00 | at Function.Module._load (node:internal/modules/cjs/loader:778:27)
| 2023-02-16T14:52:40.429+00:00 | at Module.require (node:internal/modules/cjs/loader:1005:19)
| 2023-02-16T14:52:40.429+00:00 | at require (node:internal/modules/cjs/helpers:102:18)
| 2023-02-16T14:52:40.429+00:00 | at Object.<anonymous> (/tmp/app/server.js:2:20)
| 2023-02-16T14:52:40.429+00:00 | at Module._compile (node:internal/modules/cjs/loader:1105:14)
| 2023-02-16T14:52:40.429+00:00 | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
| 2023-02-16T14:52:40.429+00:00 | at Module.load (node:internal/modules/cjs/loader:981:32)
| 2023-02-16T14:52:40.429+00:00 | at Function.Module._load (node:internal/modules/cjs/loader:822:12)
| 2023-02-16T14:52:40.429+00:00 | at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) {
| 2023-02-16T14:52:40.429+00:00 | code: 'MODULE_NOT_FOUND',
| 2023-02-16T14:52:40.429+00:00 | requireStack: [ '/tmp/app/server.js' ]
| 2023-02-16T14:52:40.429+00:00 | }
I've trying almost everything this thread has mentioned so far:
NOTE: I also tried almost every combination of them. If anyone can suggest a different fix I'll greatly appreciate it, I'm walking in circles for too long 😑 |
Beta Was this translation helpful? Give feedback.
-
@Yagogc was having the same issue using nx monorepo with setting What I ended up doing was add the following to my output: 'standalone',
// https://nextjs.org/docs/advanced-features/output-file-tracing#caveats
experimental: {
// this includes files from the monorepo base two directories up
outputFileTracingRoot: path.join(__dirname, '../../'),
}, Which is basically the same setup that @matthewdfleming described which I noticed too late already after going through a couple of tests and got it working. |
Beta Was this translation helpful? Give feedback.
-
I have the same issue still, running 13.2.4
|
Beta Was this translation helpful? Give feedback.
-
I'm seeing this issue when using PNPM + a prebuilt deployment from a monorepo. Here's an extremely minimal reproduction repo for my case. See the readme for instructions on how to reproduce it: https://github.com/trappar/next-vercel-ssr-bug Maybe I'm doing something wrong to cause the issue here? |
Beta Was this translation helpful? Give feedback.
-
I removed
to the next.config.js file. I am using the |
Beta Was this translation helpful? Give feedback.
-
This issue is resolved for me after talking with @TooTallNate Fix for me was the following:
|
Beta Was this translation helpful? Give feedback.
-
Ran into this issue when dockerizing a nextjs app inside of a yarn workspace. The solution suggested above, copying the node_modules to the build fixed it for me. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
const path = require("path"); /** @type {import('next').NextConfig} */ adding this to next config worked don't forget to bring in path |
Beta Was this translation helpful? Give feedback.
-
I'm seeing this issue with next Using "@apollo/client": "3.9.11",
"@apollo/experimental-nextjs-app-support": "0.10.0",
"next": "14.1.0",
"react": "18.2.0",
"react-dom": "18.2.0", |
Beta Was this translation helpful? Give feedback.
-
Would it be possible to add some additional debugging, or explanation of what the vercel cli is looking for when building? Otherwise debugging / fixing errors becomes impossible. I have a monorepo setup with bazel. Things run locally, and the .next folder is created, but the I tried various solutions suggested in this thread, but nothing seems to work. The problem is that this is a black box, and it's not clear what is running, in which directory, where is it looking for files, etc. Would love to have additional logging! |
Beta Was this translation helpful? Give feedback.
Hi, we have identified an issue with turbo/nx cache and have deployed a fix for this, please redeploy your project and give it a try!