question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deploying a small sized Next.js project with SSR

See original GitHub issue

Hello team! I need urgent help. I am using NX monorepo for my current project where I have a next.js dashboard app located in /apps/dashboard , this app is widely implementing the SSR feature using getServerSideProps. I am trying to deploy this next.js app (dashboard) to production. I have used yarn build and yarn serve and everything went smoothly, however, I want to deploy it without having all the node_modules and the whole monorepo. I tried making a standalone build but that didn’t work, i got the same issues in here. Currently I want to deploy to Azure using a docker container, and I am really looking for a small size docker image. I need your help. Thanks !

Current Behavior

Can’t build a standalone next.js app. Unavoidable bloated node_modules folder.

Expected Behavior

Steps to Reproduce

Build the app using nx build yarn nx run dashboard:build --configuration=production

the build output will be in dist/apps/dashboard , however, this can’t be run by it self, it needs the whole nx monorepo packages. my run command yarn nx run dashboard:serve --configuration=production

This issue may not be prioritized if details are not provided to help us reproduce the issue.

Failure Logs

Environment

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
ndcunninghamcommented, Jun 17, 2022

Hey! Thanks for opening this. When it comes to deploying next apps, using Nx, Vercel is first class. Here is our guide for it: https://nx.dev/guides/deploy-nextjs-to-vercel#deploying-nextjs-applications-to-vercel Artifacts inside dist/apps/<app> are self-containted so usually you should be able to deploy that folder and run npx next start from it. We will look into other deployment targets, as currently there maybe a few issues left to be ironed out.

3reactions
kaceyclevelandcommented, Jun 25, 2022

For building a docker image with NX and NextJS and to maintain a small image size, I would recommend using this in your next.config.js:

experimental: {
    // See: https://github.com/nrwl/nx/issues/9017
    outputStandalone: true,
    outputFileTracingRoot: path.join(__dirname, '../../'),
  },
  

This should dramatically decrease your image size and in addition, you do not need to do a npm install in the built standalone build. The package.json is incorrect however the image size is small and the application should work without npm install so the package.json should not matter really. For the Docker file, you can basically copy the standalone files over and that should be it.

FROM node:16-alpine
WORKDIR /app

ENV NODE_ENV=production

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

COPY --chown=nextjs:nodejs dist/apps/public-app/.next/standalone .
COPY --chown=nextjs:nodejs apps/public-app/public ./apps/public-app/public

USER nextjs

EXPOSE 3100

ENV PORT 3100

CMD ["node", "apps/public-app/server.js"]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to host a Next.js web app with server-side rendering ...
Amplify Init · command from the root of your app directory · Enter a name for the project pokessr · Name: pokessr ·...
Read more >
Going to Production - Next.js
Before taking your Next.js application to production, here are some ... Code Editor to view import costs and sizes; Finding alternative smaller packages ......
Read more >
NextJS 12.1 SSR & SSG: Everything you need to know
Server Side Rendering, Static Site Generation, and Client Side Rendering are all supported with NextJS 12.1.
Read more >
How to deploy NextJs (SSR) using "Output File Tracing ...
Below are the steps of solution how I deploy my Next.js project to Azure App Service with the "Output File Tracing" feature. Project...
Read more >
Best practices to increase the speed for Next.js apps
SSR will also improve application performance, especially on ... This is a two-step process: 1) resize the image to a smaller size and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found