Business

Speeding Up Node & ReactJS Build Times

Speeding up Node or React build times on an EC2 instance involves optimizing your build process, leveraging the instance’s resources efficiently, and potentially tweaking your environment. Below are practical steps to reduce build times:

1. Optimize Your Node.js Environment

  • Use the Latest Node.js Version: Ensure you’re using a recent LTS version of Node.js (e.g., 18.x or 20.x as of March 2025). Newer versions often include performance improvements.
  curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
  sudo apt-get install -y nodejs
  • Increase Node.js Memory Limit: Prevent crashes or slowdowns due to memory constraints by increasing the heap size.
  export NODE_OPTIONS="--max-old-space-size=4096"  # Adjust based on your app's needs

2. Parallelize and Cache Builds

  • Use npm or yarn with Caching:
  • If using npm, enable the built-in cache:
    bash npm install --prefer-offline --cache /tmp/npm-cache
  • If using yarn, it caches dependencies by default. Ensure your node_modules is reused between builds:
    bash yarn install --frozen-lockfile
  • Parallelize Tasks with concurrently:
    Install concurrently to run multiple scripts in parallel (e.g., building CSS and JS simultaneously):
  npm install -g concurrently
  concurrently "npm run build:css" "npm run build:js"
  • Leverage esbuild for Faster Bundling:
    Replace Webpack or other bundlers with esbuild, which is significantly faster:
  npm install esbuild --save-dev

Example esbuild script:

  require('esbuild').build({
    entryPoints: ['src/index.js'],
    bundle: true,
    outfile: 'dist/bundle.js',
    minify: true,
    target: 'esnext',
  }).catch(() => process.exit(1));

3. Optimize React Build Process

  • Use react-scripts Optimizations:
    If using Create React App (CRA), ensure production builds are optimized:
  npm run build

For faster builds, consider ejecting and customizing Webpack, or switch to a faster alternative like Vite.

  • Switch to Vite:
    Vite is a modern build tool that’s much faster than CRA or Webpack:
  npm create vite@latest my-app -- --template react
  cd my-app
  npm install
  npm run build
  • Reduce Bundle Size:
  • Analyze your bundle with webpack-bundle-analyzer or vite-bundle-visualizer to remove unnecessary dependencies.
  • Use dynamic imports (React.lazy) for code-splitting.

4. Utilize EC2 Instance Resources Efficiently

  • Maximize CPU Usage:
  • The t2.large has 2 vCPUs. Ensure your build process uses both by enabling parallel tasks (e.g., Webpack’s thread-loader or esbuild’s parallel mode).
  • Example for Webpack: module.exports = { module: { rules: [ { test: /\.js$/, use: ['thread-loader', 'babel-loader'], }, ], }, };
  • Increase Swap Space:
    If RAM (8 GiB) is a bottleneck, add swap space to prevent crashes:
  sudo fallocate -l 4G /swapfile
  sudo chmod 600 /swapfile
  sudo mkswap /swapfile
  sudo swapon /swapfile
  sudo sysctl vm.swappiness=10
  • Upgrade Instance Type (if possible):
    A t2.large is relatively modest. If budget allows, consider a t3.large or c5.large for more CPU power during builds.

5. Use Build Caching with CI/CD or Local Tools

  • Cache node_modules Locally:
    Store node_modules in an S3 bucket or EBS volume and reuse it:
  aws s3 cp s3://my-bucket/node_modules.tar.gz .
  tar -xzf node_modules.tar.gz
  npm ci  # Faster than npm install
  • Use Docker with Layer Caching:
    If your build runs in a container, cache layers:
  FROM node:20
  WORKDIR /app
  COPY package.json yarn.lock ./
  RUN yarn install --frozen-lockfile
  COPY . .
  RUN yarn build

6. Profile and Debug Build Bottlenecks

  • Time Your Build:
    Use the time command to identify slow steps:
  time npm run build
  • Enable Webpack Stats:
    Generate a stats file to analyze:
  npm run build -- --profile --json > stats.json

Then use a tool like webpack-analyzer to visualize.


Example Workflow

Here’s a combined approach:

  1. Install dependencies with caching:
   yarn install --frozen-lockfile
  1. Build with esbuild or Vite:
   npx esbuild src/index.js --bundle --outfile=dist/bundle.js --minify
  1. Run in parallel if needed:
   concurrently "npx esbuild src/index.js --bundle --outfile=dist/app.js" "npx esbuild src/worker.js --bundle --outfile=dist/worker.js"

Final Tips

  • Monitor Resource Usage: Use htop or free -m during builds to check CPU/memory usage.
  • Test Incrementally: If builds are still slow, test each optimization individually to see what works best for your app.

Be strong 💪🏼


Discover more from Ido Green

Subscribe to get the latest posts sent to your email.

Standard