Dockerfile 없이 소스 배포를 사용하는 경우 Cloud Run에서 최적화를 실행합니다.
코드 번들
번들러는 로드 시간을 단축하기 위해 JavaScript 소스 파일의 레이아웃을 최적화하는 빌드 도구입니다. 일반적인 번들러 최적화에는 트리 쉐이킹, 압축, 작은 파일 병합이 포함됩니다. 번들러는 번들의 총 크기를 대폭 줄이고 파일 읽기 요청 수를 줄입니다. 일반적인 JavaScript 번들러는 esbuild, webpack, rollup입니다.
종속 항목 지연 로드
시작 시 Cloud Run은 코드가 원격 위치에서 로드하는 각 파일을 스트리밍합니다. 로컬 파일 시스템과 비교할 때 원격 위치를 사용하면 파일을 읽을 때마다 지연 시간이 추가로 발생합니다. Node.js 패키지는 간접 종속 항목이 있는 파일을 많이 사용할 수 있습니다. 시작 시 모든 종속 항목을 가져오는 대신 서버가 시작하는 데 필요한 종속 항목만 로드하고 동적 가져오기를 사용하여 다른 종속 항목을 지연 로드하는 것이 좋습니다.
예를 들어 모듈 상단에서 import { Storage } from '@google-cloud/storage' 같은 가져오기를 사용하는 대신 가져온 객체가 필요할 때 함수에서 import()를 사용합니다.
Node.js 기본 제공 HTTP 서버의 기본 제한 시간은 2분입니다. Cloud Run 서비스의 요청 제한 시간이 더 긴 경우 server.setTimeout(msecs)를 사용하여 제한 시간을 수정합니다. Express 등 Node.js 서버에 빌드된 프레임워크의 제한 시간도 수정해야 합니다. Node.js에서 무제한 제한 시간을 설정하고 Cloud Run의 기본 제공 제한 시간을 사용하려면 server.setTimeout(0)을 사용합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-15(UTC)"],[],[],null,["# Optimize Node.js applications for Cloud Run\n\nThis guide describes optimizations for Cloud Run services\nwritten in JavaScript or TypeScript, running on the Node.js runtime. The\ninformation on this page supplements the [general development tips](/run/docs/tips/general), which also apply to Node.js.\n\nOptimize startup time\n---------------------\n\nBy optimizing startup time, you can reduce latency, improve responsiveness, and\nachieve effective cost optimization. This section describes different ways to\noptimize startup time.\n\n### Start your app using `node` instead of `npm`\n\nStart your application directly using `node index.js` instead of `npm start`,\nas `npm` adds extra latency.\n\nTo start your application with `node index.js`, follow either of these methods:\n\n- Use `CMD node index.js` in your Dockerfile, for example:\n\n CMD node index.js\n\n- Set `node index.js` as your entrypoint. Run the following command\n using the Google Cloud CLI:\n\n gcloud run deploy \u003cvar translate=\"no\"\u003eSERVICE\u003c/var\u003e --command \"node index.js\"\n\n For more information and options for configuring entrypoints, see [Configure containers for services](/run/docs/configuring/services/containers#configure-entrypoint-services).\n\nIf you use source deployments without a Dockerfile, Cloud Run performs the\noptimization.\n\n### Bundle your code\n\nA bundler is a build tool that optimizes the layout of your JavaScript source\nfiles for faster load times. Some common bundler optimizations include tree shaking, minification, and coalescing of small files. Bundlers significantly reduce the\ntotal size of the bundle and reduce the number of file read requests. Common JavaScript bundlers are [esbuild](https://esbuild.github.io/), [webpack](https://webpack.js.org/), and [rollup](https://rollupjs.org/).\n\n### Lazy load dependencies\n\nAt startup, Cloud Run streams each file that your code loads from a\nremote location. Compared to a local file system, using a remote location leads to additional latency each time\na file is read. Node.js packages might use\nmany files with indirect dependencies. Instead of importing all dependencies at\nstartup, we recommended that you only load dependencies that are required for\nyour server to start, and lazy load the other dependencies with [dynamic imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import).\n\nFor example, instead of using import at the top of your module, such as\n`import { Storage } from '@google-cloud/storage'`, use `import()` in the function\nwhen it requires the imported object, for example: \n\n const { Storage } = await import('@google-cloud/storage');\n\nTo identify modules that load during startup and the time taken by each module\nto load on your machine, run the following command: \n\n node --trace-event-categories node.module_timer --trace-event-file-pattern 'trace-events.log' index.js\n\n### Configure timeout\n\nThe Node.js built-in HTTP server has a [default timeout](https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_server_settimeout_msecs_callback) of two minutes. If the\nrequest timeout of your Cloud Run service is longer, modify the timeout\nusing `server.setTimeout(msecs)`. You must also modify timeout for frameworks\nthat are built on the Node.js server, such as [Express](https://expressjs.com/). To\nachieve an unlimited timeout in Node.js and to rely on Cloud Run's built-in\ntimeout, use `server.setTimeout(0)`."]]