We use cookies to ensure you get the best experience on our website. Read our privacy policy

Skip to main content
NPM Packages coming out of a factory conveyor belt
Home Blog Cloudflare

Production only NPM Builds for Cloudflare Pages

Make your Cloudflare Pages deployments faster and more efficient by configuring your build process to use only production dependencies.

Published on:
Clock Time to complete: 10 minutes

When deploying websites to Cloudflare Pages, you don’t need development dependencies like testing frameworks and linting tools in your production build. This guide shows you how to configure Cloudflare Pages to install only necessary production dependencies, resulting in faster builds and cleaner deployments.

Packages required for your application to run in production

  • Listed under dependencies in package.json
  • Examples: React, Vue, Express

Packages only needed during development such as testing frameworks, linting tools, and type definitions

  • Listed under devDependencies in package.json
  • Examples: Jest, ESLint, TypeScript, Playwright

By default, Cloudflare Pages installs all dependencies during the build process. We can optimize this by installing only production dependencies.

First, ensure your package.json has dependencies properly categorized:

"dependencies": {
"astro": "^3.0.0",
"react": "^18.0.0"
},
"devDependencies": {
"jest": "^29.0.0",
"eslint": "^8.0.0"
}

Add a dedicated production build script to your package.json:

{
"scripts": {
"build:production": "npm clean-install --omit=dev && astro build",
"build:development": "astro build"
}
}

The clean-install --omit=dev flag ensures only production dependencies are installed.

  1. Add an environment variable:

    • Name: SKIP_DEPENDENCY_INSTALL
    • Value: 1
  2. Set the build command:

    • Command: npm run build:production
Cloudflare Pages build settings

This configuration tells Cloudflare to:

  • Skip the default dependency installation
  • Use your custom build command that only installs production dependencies

If done correctly you will see the following in the build logs:

Cloudflare Pages Build Logs
SKIP_DEPENDENCY_INSTALL is present in environment. Skipping automatic dependency installation.
Executing user command: npm run build:production

Test the build locally to ensure it works as expected and all required dependencies are in the “dependencies” section of your package.json. If the build succeeds locally, it should work on Cloudflare Pages as well.

  1. Remove the node_modules directory
  2. Run npm run build:production
  3. Verify that your application builds successfully
  4. Check that development tools are not installed
  1. Missing Dependencies: Check if any production dependencies were incorrectly listed under devDependencies
  2. Build Script Errors: Ensure your build script includes all necessary steps in the correct order
  3. Environment Variables: Verify that any required environment variables are properly set in your Cloudflare Pages project settings
  • Check Circle

    Regularly audit your dependencies to ensure proper categorization

  • Check Circle

    Keep your package.json organized and up-to-date

  • Check Circle

    Document any special build requirements in your project’s README

  • Check Circle

    Test production builds locally before deploying

By following these steps, you’ll have a more efficient build process that only includes the packages necessary for your production environment.

Related Articles

Read more Cloudflare articles
North Star Themes Logo

Subscribe to our newsletter

Get the latest AstroJS tips and tricks right to your inbox

Email: [email protected]

© 2025 North Star Themes

Web Kit Provided By North Star Themes