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.

[nextjs] `nx serve project_name --prod` - does it support all features of `next start` ?

See original GitHub issue

Documentation issue

  • Reporting a typo
  • Reporting a documentation bug
  • Documentation improvement
  • Documentation feedback

Is there a specific documentation page you are reporting?

https://nx.dev/packages/next

Additional context or description

We are using the nx nextjs package, and its working great for our development needs. Now we are trying to do self hosting in a nodejs environment and once we do nx build and then nx serve --prod, it runs the project fine. Note that we are not doing static html export. It will be a regular non-static site.

Now in nextjs documentation, https://nextjs.org/docs/deployment, it says the next start server supports all the advanced features that nextjs offers. We are wondering whether running our nx nextjs project using nx serve --prod provides exactly the same functionality that next start provides. We looked into the nx executors for nextjs, https://github.com/nrwl/nx/blob/master/packages/next/src/executors/server/lib/default-server.ts, and it seems to be spinning up an express server.

Hence our question is, after running nx build project_name, when serving via nx serve project_name --prod - does it support all features of next start ? If not, what is the recommended way to have all the extra features of next start ? Note that we have read about vercel deployment (eg https://nx.dev/recipes/other/deploy-nextjs-to-vercel) but we are not interested in vercel for this exercise.

Many thanks.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
marko-hologramcommented, Oct 19, 2022

@bappy004 hey, yeah this is basically it. Only step extra is rm -rf node_modules. There is no need to remove root level node_modules.

And as I’ve mentioned, if you use “standalone” output from Next.js, node_modules will already be output into apps/your-app/.next/standalone. That way you can avoid this step where you install bunch of node modules again because you already have them installed at root level of your repo during build.

// This step can be avoided if output is set to "standalone" in next.config.js https://nextjs.org/docs/advanced-features/output-file-tracing
cd  dist/apps/test-app
yarn install --production=true
1reaction
marko-hologramcommented, Oct 18, 2022

However, if I use nx serve --prod, it works just fine.

It’s because it’s started from the root of the repo which contains all the needed dependencies (node_modules) to start the app. In production when you deploy the app, you don’t want to deploy your whole NX monorepo.

So do you mean I have to cd to the dist/apps/project_name folder and then next start

Yes and depending on your Next.js setup you might need to run npm install (or yarn install) to install all required dependencies for that Next.js project before running that next start command.

If for example you’ve used “standalone” output of Next.js, you should get all node_modules and get a server.js file that you can just run using node server.js if you wanted. https://nextjs.org/docs/advanced-features/output-file-tracing


Once you’ve built the app using NX, you want to handle the rest of the process as if you didn’t have NX at all. It’s just a matter of deploying that Next.js app after NX has built it and there should be tutorials online on how to do that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run nrwl/nx production build of web app on localhost ...
This server supports all features of Next.js. That means that in NX you want to build your project: nx build project-name. Then navigate ......
Read more >
Nx Now Supports Next.js
Nx is a set of extensible dev tools for monorepos. And in its latest release we have added first-class Next.js support. Next.js is...
Read more >
Deploying Next.js Application on Vercel, Heroku, and a ...
It means that every developer that wants to ship a new version of an application should have access to production server.
Read more >
Getting Started with Monorepo with Nx Nrwl - Rupesh Tiwari
Do you have a lot of JavaScript projects using GitHub repositories? How are you managing dependencies among those projects?
Read more >
@nrwl/next | Nx
This will start the dev server at http://localhost:4200. To serve a Next.js application for production, add the --prod flag to the serve command:....
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