nx: Next.js 12 Output File Tracing Fails to Build
Current Behavior
- create a new nx workspace w/ a next.js app
- turn on next.js output file tracing
- run
nx build --prod - build fails w/ the error message saying
dist/apps/haha/.next/standalone/apps/haha/server.jsdoesn’t exist (it doesn’t)
Expected Behavior
The standalone folder should contain a server.js file.
Steps to Reproduce
I’ve created a repro repo here. Clone & run yarn nx build --prod to see error message.
Failure Logs
ENOENT: no such file or directory, open '/nx_next_standalone_repro/dist/apps/haha/.next/standalone/apps/haha/server.js'
Environment
Output of next info
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:29:10 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T8101
Binaries:
Node: 16.13.1
npm: 8.1.2
Yarn: 1.22.17
pnpm: N/A
Relevant packages:
next: 12.1.0
react: 17.0.2
react-dom: 17.0.2
Output of nx report
Node : 16.13.1
OS : darwin arm64
yarn : 1.22.17
nx : 13.8.2
@nrwl/angular : undefined
@nrwl/cli : 13.8.2
@nrwl/cypress : 13.8.2
@nrwl/detox : undefined
@nrwl/devkit : 13.8.2
@nrwl/eslint-plugin-nx : 13.8.2
@nrwl/express : undefined
@nrwl/jest : 13.8.2
@nrwl/js : 13.8.2
@nrwl/linter : 13.8.2
@nrwl/nest : undefined
@nrwl/next : 13.8.2
@nrwl/node : undefined
@nrwl/nx-cloud : undefined
@nrwl/react : 13.8.2
@nrwl/react-native : undefined
@nrwl/schematics : undefined
@nrwl/storybook : 13.8.2
@nrwl/tao : 13.8.2
@nrwl/web : 13.8.2
@nrwl/workspace : 13.8.2
typescript : 4.5.5
rxjs : 6.6.7
---------------------------------------
Community plugins:
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 38
- Comments: 70 (7 by maintainers)
Commits related to this issue
- configure Nx next project for standalone via https://github.com/nrwl/nx/issues/9017\#issuecomment-1284740346 — committed to kevinold/nx-repo-example by kevinold 2 years ago
- Ensure folder exists for standalone server (#44745) When using standalone output in a monorepo, nextjs will sometimes throw: `> NX ENOENT: no such file or directory, open '/dist/apps/my-app/.next... — committed to vercel/next.js by ianldgs a year ago
- Ensure folder exists for standalone server (#44745) When using standalone output in a monorepo, nextjs will sometimes throw: `> NX ENOENT: no such file or directory, open '/dist/apps/my-app/.next... — committed to jankaifer/next.js by ianldgs a year ago
Thanks to the comments above here’s what worked for me on “nx”: “14.8.3” and “next”: “12.3.1”.
As Yizhachok said I had to add the following, although I had to add ‘output’ outside of experimental. Also needed to add
const path = require('path');to the top of the file.then in pages/index I added
This wasn’t working for me initially so the final thing I had to do was delete my dist folder that I had generated previously and then run the build command with the --skip-nx-cache argument so:
nx build shop-next --skip-nx-cache. This finally worked and I was able to run the server file in dist/apps/my-app/.next/standalone/apps/my-app/server.jsI think a more proper solution by NX is really necessary as without these workarounds the docker image is 1.5 GB for me.
Just don’t ask how I investigated this and how much time I spend to find out how to fix this. ⚠️ WARNING ⚠️ Next text for people who believe in magic, skeptics, please stop read this text right now. I warned you.
Steps to get working repo for standalone Next.js server with build using NX CLI.
Init example workspace (or use your own)
npx create-nx-workspace@latest --preset=next(tested on v14.1.9 workspace)next.config.jsfile and add to config:Preparation finished, now let’s make real magic
Go to
pages/index.tsx(or any page file) and add this lines (don’t ask why, this is MAGIC):Really can be any js file that can be resolved from current location (even empty). This code enough to insert only once.
Show magic to the world
Run
npm run build. That’s it! No errors!Bonus
This is a VERY SIMPLE Docker config that show how to work with this build.
Thanks to @bboyz269 for working example, from his repository I found the issue why build didn’t work on clean project. His repo works because of
next-i18nextlib code.Thanks everyone for your comments on this issue!
Wrapping up here what worked for me, as it took me a few hours to get it done. Make sure to change
your-appto your app/project’s name. The steps below consider that your workspace’s layout is the default “empty” withapps/andlibs/folders in the workspace root directory.A few considerations:
Build and deploy a Next.js app in
standalonemode from an Nx workspace with DockerSee more
package.json— for referenceworkspace.json— for referenceapp/your-app/project.json— for reference only, generated withyarn nx g @nrwl/next:application your-appoutputStandalone: true, addpath.resolve('./next.config.js')toapps/your-app/pages/index.tsxas suggested by @Yizhachokapps/your-app/next.config.jsas suggested by @LudovicPelleDMSRun
yarn nx build your-appCreate a
Dockerfilein the workspace root directory (I haven’t managed to get it working anywhere else yet) as suggested by @always-maapRun
docker buildx build -t ghcr.io/your-github-username/your-app .Run
docker run -p 3000:3000 ghcr.io/your-github-username/your-appOpen
http://localhost:3000and check if it works.I post mine maybe it helps
For those running next.js in docker this is a groundbreaking feature so I would be very happy to have this issue resolved. The docker image will reduce in size by a lot.
In our case it will also reduce the build time very much as we are doing what we can to have the docker image as small as possible, and in order to achieve that we have to do npm install twice, first for the build, and then again for the generated package.json file for this app.
Changing the outputPath in production configuration works for me:
"configurations": { "production": { "outputPath": "apps/<my-app>/dist" } }Ornpx nx run my-app:build --configuration=production --outputPath="apps/<my-app>/dist"Can confirm this fixed everything for me 😂
The steps by @moatorres work great except I had to change:
to:
And in
13.9.7, reproduce the issue by cloning the following repository and runErrror message:
This is now fixed with Nx version
16.3.0This is a huge problem for us. We don’t use any deployment platform like vercel or netlify. We deploy our front-end to a kubernetes cluster, so we need to be able to dockerize our next.js applications. Any comment from nx team on the status of the issue? This has been open for more than a year.
Unfortunately the above workaround did not work for me while using
nx: 14.5.15andnext:12.2.3. I’m just going to go without the new functionality for now until their is support for such with nx.Hey all,
My standalone build outputs this server.js file
However it only outputs this next-server.js file in the node_modules folder
The server fails to start because required(“./initialize-require-hook”); doesn’t exist in the node_modules.
Here is my next config file
Am I missing something?
First of all thank you for your work, so I also came across the same problem, I was able to find a Hack similar to the comments, it is indeed a problem of Path between Nx (root, outputPath) and the standalone of next,
and
finish with
this standalone functionality is a real “plus value” for the production of minimal image (docker, k8s, …) I hope you will be able to look into it quickly, thank you
Hi all, I was wondering if there was an official fix for this. I’m currently leaving the build inside the Next.js directory (I just added Nx; not using
@nrwl/next), but I was able to get it working with this Dockerfile (app is nameddashboard):Pretty weird combination of folders/files, but I hope it helps someone in the meantime, or reveals the source of the issue.
Probably Nx v16.3.0 will fix this issue thanks to the direct use of nextjs cli and API instead of nx wrappers
I’m not implying that either, but since I did share workarounds above for both Next 12 and 13, I find it fairly difficult to help you without any inputs other than “I’ve been trying to resolve this all day without luck. Is there any way to get eyes on this?” 😕
What have you tried? What is the structure of your workspace? Are you using the default ‘dist’ path? What error messages are you catching? Are you using Next 12 or 13? Have you added the suggested “import path from ‘path’”? On which file? I understand your frustration. Hence why I’m here replying to you.
Has anyone else noticed issues when it comes to the automatic environment variable expansion in the
.envfiles when using theoutputStandaloneandoutputFileTracingRootoptions?Example from the docs for clarity:
Everything else seems ok, but my pages are dying because of incorrect configuration values (my env vars seem to be passed along as simple strings like
$HOSTNAMEinstead of actually being substituted)It seems this issue won’t be resolved for quite some time. I created a sample repo nx-next-i18next-standalone building a minimal docker image that works with multiple apps nx workspace. Thanks to all comments from this thread!
There’s also next-i18next integration if you’re interested.
I looked into this a bit more last weekend. It looks like the root-cause might be a race-condition of some sort. The step that writes the tiny server to the
server.jsfile located here is throwing an error because it thinks that file hasn’t been created yet.On next.js’ side, output-file-tracing is working fine (if you
create-next-appand enableoutputFileTracing, everything works as expected). So I’m thinking somewhere around this step, something in nx is maybe removing thatserver.jsfile.Hope that helps.
I was unable to get standalone builds working until I followed this. There is no reason why it should be this difficult.
Looks like the server folder is not copied properly.
After running
nx build myapp --skip-nx-cache, theapps/myapp/.next/standalone/node_modules/next/dist/serverfolder has significantly less number of files as compared to the a non nx nextjs build created vianpx create-next-app --example with-docker nextjs-docker.Here are the contents for
apps/myapp/.next/standalone/node_modules/next/dist/serverContents for with-docker for
.next/standalone/node_modules/next/dist/serverUnfortunately, none of workarounds work with
nx 14.8.1andnext 12.3.1. I’ve also tried to debug Next CLI and change the output folder, but it looks like .nft files are not generated correctly. For sure the bug is present in NX and should be fixed, as Next standalone works fine.@Yizhachok Yes, I also tried it on a fresh project, and it absolutely works. An interesting tidbit is that resolving
./next.config.jsmakes the build succeed, but resolving../next.config.js, i.e the correct path relative topages/index.tsx, fails the build.As for my own project I’m guessing I accidentally “solved” this problem in a similar fashion along the way without realizing it, since I was still using the outputPath-hack so the original error was obscured.
Yes, you are right. But, I will run this command just in the CI step of my workflow and I’ve made some changes in my Dockerfile to get just the files from this folder.
I know it is not the best solution, but it was the only way that I could get this working.
One thing that’s interesting: at one point I hard-coded the
distDirvalue as an absolute-path inside the next.js codebase (for that build process), and the correct standalone output was generated. Albeit, in the wrong directory lol it put the build output inapps/<my-app>/<absolute-path-from-home-dir>/<dist-folder-with-correct-standalone>.This actually does seem to be a dependency mismatch issue. The last next.js version that
output-file-tracingworks for is12.0.7(which has a known security vulnerability according to snyk).Does Nx document supported next.js versions anywhere? Just saw an issue asking for support for
next.js@12.1.0, so I’m assuming its not officially supported?