Optimize Node.js applications for Cloud Run

This guide describes optimizations for Cloud Run serviceswritten in JavaScript or TypeScript, running on the Node.js runtime. Theinformation on this page supplements thegeneral development tips, which also apply to Node.js.

Optimize startup time

By optimizing startup time, you can reduce latency, improve responsiveness, andachieve effective cost optimization. This section describes different ways tooptimize startup time.

Start your app usingnode instead ofnpm

Start your application directly usingnode index.js instead ofnpm start,asnpm adds extra latency.

To start your application withnode index.js, follow either of these methods:

  • UseCMD node index.js in your Dockerfile, for example:

    CMDnodeindex.js
  • Setnode index.js as your entrypoint. Run the following commandusing the Google Cloud CLI:

    gcloud run deploySERVICE --command "node index.js"

    For more information and options for configuring entrypoints, seeConfigure containers for services.

If you use source deployments without a Dockerfile, Cloud Run performs theoptimization.

Bundle your code

A bundler is a build tool that optimizes the layout of your JavaScript sourcefiles for faster load times. Some common bundler optimizations include tree shaking, minification, and coalescing of small files. Bundlers significantly reduce thetotal size of the bundle and reduce the number of file read requests. Common JavaScript bundlers areesbuild,webpack, androllup.

Lazy load dependencies

At startup, Cloud Run streams each file that your code loads from aremote location. Compared to a local file system, using a remote location leads to additional latency each timea file is read. Node.js packages might usemany files with indirect dependencies. Instead of importing all dependencies atstartup, we recommended that you only load dependencies that are required foryour server to start, and lazy load the other dependencies withdynamic imports.

For example, instead of using import at the top of your module, such asimport { Storage } from '@google-cloud/storage', useimport() in the functionwhen it requires the imported object, for example:

const{Storage}=awaitimport('@google-cloud/storage');

To identify modules that load during startup and the time taken by each moduleto load on your machine, run the following command:

node --trace-event-categories node.module_timer --trace-event-file-pattern 'trace-events.log' index.js

Configure timeout

The Node.js built-in HTTP server has adefault timeout of two minutes. If therequest timeout of your Cloud Run service is longer, modify the timeoutusingserver.setTimeout(msecs). You must also modify timeout for frameworksthat are built on the Node.js server, such asExpress. Toachieve an unlimited timeout in Node.js and to rely on Cloud Run's built-intimeout, useserver.setTimeout(0).

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-02-19 UTC.