Why NODE_ENV=production Can Break a Next.js Build (And How to Fix It)

Why NODE_ENV=production Can Break a Next.js Build (And How to Fix It)

NODE_ENV=production is supposed to mean “run fast and lean” — but set it at the wrong moment and your Next.js build exits with a cryptic module-not-found error. This article explains exactly what goes wrong, why it happens, and how to deploy Next.js safely on CM Cloud App Hosting.


What NODE_ENV=production Actually Does

Node.js and npm both read NODE_ENV to decide how to behave. When the value is production, two things happen simultaneously:

  1. npm install (or yarn install) skips every package listed under devDependencies in your package.json.
  2. Libraries like React and webpack switch into their trimmed, production-only code paths.

That is the intended behaviour for running the app. The problem is that building a Next.js project is a compile step that needs dev tools — things like @next/swc, postcss, tailwindcss, eslint, and your custom Babel or TypeScript config — all of which live in devDependencies.

If NODE_ENV is already production when npm install runs, those packages are never installed. Then next build tries to import them and fails.


The Exact Sequence That Causes the Break

Here is the typical order of events on a misconfigured deploy pipeline:

  1. CI/CD system exports NODE_ENV=production as a global environment variable (a common “best practice” tip found on generic DevOps tutorials).
  2. The build step runs npm install — npm sees NODE_ENV=production and prunes devDependencies.
  3. The next step runs next build — it immediately fails because postcss, tailwindcss, or the SWC binary is missing.
  4. The error message says something like: Cannot find module 'tailwindcss' or Error: Cannot find module '@next/swc-linux-x64-gnu'.

The fix is not complicated — but you need to understand where to set the variable.


How to Fix It: Separate Build Env from Runtime Env

Option 1 — Set NODE_ENV only at runtime, not at build time

Do not export NODE_ENV=production in your CI step that runs npm install and next build. Let it default to development (or unset) during the build, then set it to production only in the command that starts the server:

# Build step (NODE_ENV unset or 'development')
npm install
npm run build

# Start step (NODE_ENV=production)
NODE_ENV=production node server.js
# or
NODE_ENV=production npm start

Option 2 — Install with npm ci –include=dev

If your pipeline must have NODE_ENV=production set globally, override the install behaviour explicitly:

npm ci --include=dev

This tells npm to install devDependencies regardless of what NODE_ENV says. It is an npm v7+ flag.

Option 3 — Move build tools to dependencies (not recommended long-term)

Moving tailwindcss, postcss, and typescript from devDependencies to dependencies will stop the crash — but it bloats your production node_modules and is the wrong signal semantically. Use Options 1 or 2 instead.


Next.js-Specific Gotcha: NEXT_PUBLIC_ vs Runtime ENV

While fixing your NODE_ENV issue, do not confuse it with NEXT_PUBLIC_ variables. Next.js bakes any variable prefixed NEXT_PUBLIC_ into the client bundle at build time. That means:

  • They must be present in the build environment, not just the runtime environment.
  • Changing them after the build has no effect without a rebuild.
  • They are visible to end users in the browser bundle — never put secrets there.

Secret values (API keys, database URLs) should be plain environment variables (no NEXT_PUBLIC_ prefix) and consumed only in server-side code (getServerSideProps, API routes, Server Components).


Deploying Next.js on CM Cloud App Hosting

CM Cloud App Hosting manages the full runtime layer — nginx, systemd, process supervision — so you push code and the platform handles the rest. Here is how to configure Next.js correctly.

Step 1 — Set environment variables in the portal

In your CM Cloud portal, add your runtime environment variables (database URL, API secrets, etc.). Do not set NODE_ENV manually — CM Cloud sets it to production at the process level after the build completes.

Step 2 — Configure your deploy hook for CI/CD

CM Cloud’s CI/CD deploy hook lets you trigger a redeploy on every git push. Generate a secret URL in the portal, then add this step to your GitHub Actions workflow:

- name: Trigger CM Cloud redeploy
  run: curl -X POST ${{ secrets.CMCLOUD_DEPLOY_HOOK }}

The build runs on the CM Cloud side, where NODE_ENV is managed correctly — your devDependencies are installed during the build phase, then the runtime starts with NODE_ENV=production.

Step 3 — Verify your package.json start script

Make sure your package.json includes:

"scripts": {
  "build": "next build",
  "start": "next start -p 3000"
}

CM Cloud calls npm run build then npm start. The start command runs after the build, so NODE_ENV=production is only active at that point.

No rollback feature is available on CM Cloud App Hosting — keep a clean git history and test builds locally before pushing to production.


App Hosting Pricing Comparison

Provider Starter Price (USD) XAF EUR Key Feature
CM Cloud App Hosting $7.99/mo XAF 5,000 €7.40 Managed runtime, CI/CD hook, Node.js/Python/Laravel/PocketBase
Railway (Hobby) $5.00/mo + usage ~XAF 3,100 ~€4.60 Usage-based, bills can spike
Render (Starter) $7.00/mo ~XAF 4,370 ~€6.50 Free tier sleeps after inactivity
Hostinger (shared, promo) From ~$1.99–$6.79/mo* ~XAF 1,240–4,230 ~€1.85–€6.30 Shared server, not app runtime

*Hostinger pricing is a promotional intro rate (advertised up to 82% off). Renewal prices are unverified — check before committing.

CM Cloud is the only provider in this table showing transparent multi-currency pricing (USD, XAF, EUR) with no lock-in intro discounts. See full pricing at cmcloudhosting.com/pricing.


AI Credits — Free on Every Plan

Every CM Cloud plan includes free AI credits. Top up any time:

Pack USD XAF EUR
25 credits $1.00 XAF 500 €0.93

Quick Reference: NODE_ENV Rules for Next.js

  • During npm install — must NOT be production (or use --include=dev)
  • During next build — should be unset or development
  • During next start — set to production
  • NEXT_PUBLIC_ vars — must exist at build time; are public and baked into the bundle
  • Secret vars — server-side only, set in your hosting portal, never prefixed with NEXT_PUBLIC_

Summary

Setting NODE_ENV=production globally before npm install silently strips the devDependencies that next build depends on. The fix is simple: let NODE_ENV default to development during the install and build phases, and only activate production when starting the server.

On CM Cloud App Hosting, this is handled correctly by the platform. You push code, the deploy hook fires, the build runs with dev tools available, and the runtime starts in production mode automatically.

For questions about setting environment variables safely for Node.js apps, see our guide on App Hosting Environment Variables. Full plan details at cmcloudhosting.com/pricing.


Deploy Next.js the Right Way — From $7.99/mo

Skip the NODE_ENV headaches. CM Cloud App Hosting manages the runtime layer so your Next.js app builds and starts correctly every time.

Start deploying at cmcloudhosting.com — plans from $7.99/mo (XAF 5,000 / €7.40), no promotional bait-and-switch pricing.

Scroll to Top