Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Secara default, runtime node.js akan menjalankan npm run build jika skrip build
terdeteksi dalam file package.json. Jika Anda memerlukan kontrol tambahan pada
langkah-langkah build sebelum memulai aplikasi, Anda dapat memberikan langkah-langkah
build kustom. Langkah-langkah build kustom dapat dijalankan dengan menambahkan gcp-build dalam
file package.json Anda.
Untuk mencegah build menjalankan skrip npm run build, Anda harus:
Menambahkan skrip gcp-build dengan nilai kosong di file package.json Anda:
"gcp-build":"". Untuk detail tentang cara mengonfigurasi package.json, lihat
konfigurasi buildpack Node.js.
Menambahkan variabel lingkungan build GOOGLE_NODE_RUN_SCRIPTS
dengan nilai kosong di file app.yaml.
build_env_variables:GOOGLE_NODE_RUN_SCRIPTS:''
Untuk mengetahui detail tentang cara menentukan variabel lingkungan build, lihat bagian build_env_variables dalam file app.yaml.
Saat skrip ini dijalankan, dependensi di kolom dependencies dan
devDependencies dari file package.json Anda akan tersedia.
Setelah menjalankan langkah build kustom, App Engine akan menghapus dan membuat ulang
folder node_modules hanya dengan menginstal dependensi produksi yang dideklarasikan
di kolom dependencies pada file package.json Anda.
Skrip build kustom dapat digunakan untuk tugas pra-pemrosesan, seperti
CSS pra-pemrosesan, meminifikasi JavaScript sisi klien, atau menjalankan alat, seperti
webpack atau gulp.
Misalnya, untuk mengompilasi TypeScript ke JavaScript,
file package.json Anda mungkin akan
terlihat seperti berikut. Perhatikan skrip gcp-build:
{"name":"appengine-typescript","description":"An example TypeScript app running on Google App Engine.","version":"0.0.1","private":true,"license":"Apache Version 2.0","author":"Google Inc.","engines":{"node":"20.x"},"scripts":{"prepare":"npm run gcp-build","pretest":"npm run gcp-build","test":"c8 mocha -p -j 2 test/*.test.js --exit","lint":"gts lint","start":"node ./index.js","deploy":"gcloud app deploy","clean":"gts clean","compile":"tsc -p .","fix":"gts fix","build":"tsc -p .","gcp-build":"tsc -p ."},"dependencies":{"@typescript-eslint/parser":"^8.0.0","express":"^4.16.3"},"devDependencies":{"@types/express":"^4.17.17","@types/node":"^20.0.0","c8":"^10.0.0","chai":"^4.5.0","gts":"^5.0.0","mocha":"^10.2.0","typescript":"^5.0.0","wait-port":"^1.0.0"}}
Menyajikan file statis
Saat Anda membuat atau menyalin file selama langkah build kustom, App Engine tidak
menyalurkan file statis ini langsung dari aplikasi Anda menggunakan
static_dir
atau static_files
yang ditentukan dalam file app.yaml. Sebagai gantinya, App Engine akan mengupload
file statis ke infrastruktur penayangan di lingkungan standar
sebelum menjalankan langkah build kustom. Setiap permintaan ke file yang dihasilkan
atau disalin selama langkah build kustom akan menghasilkan
error HTTP 404 NOT FOUND.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-19 UTC."],[[["\u003cp\u003eNode.js runtime defaults to executing \u003ccode\u003enpm run build\u003c/code\u003e if a \u003ccode\u003ebuild\u003c/code\u003e script exists in \u003ccode\u003epackage.json\u003c/code\u003e, but you can implement custom build steps.\u003c/p\u003e\n"],["\u003cp\u003eTo define a custom build process, include a \u003ccode\u003egcp-build\u003c/code\u003e script in your \u003ccode\u003epackage.json\u003c/code\u003e file, which will be executed instead of \u003ccode\u003enpm run build\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eTo bypass the default \u003ccode\u003enpm run build\u003c/code\u003e execution, add an empty \u003ccode\u003egcp-build\u003c/code\u003e script in \u003ccode\u003epackage.json\u003c/code\u003e as \u003ccode\u003e"gcp-build":""\u003c/code\u003e, or use the environment variable \u003ccode\u003eGOOGLE_NODE_RUN_SCRIPTS\u003c/code\u003e with an empty value.\u003c/p\u003e\n"],["\u003cp\u003eDuring custom build steps, dependencies from both \u003ccode\u003edependencies\u003c/code\u003e and \u003ccode\u003edevDependencies\u003c/code\u003e in \u003ccode\u003epackage.json\u003c/code\u003e are available, but afterwards, App Engine only installs production dependencies listed in \u003ccode\u003edependencies\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eStatic files generated or copied during custom build steps are not served directly by App Engine using \u003ccode\u003estatic_dir\u003c/code\u003e or \u003ccode\u003estatic_files\u003c/code\u003e in \u003ccode\u003eapp.yaml\u003c/code\u003e, and accessing them will result in a \u003ccode\u003e404 NOT FOUND\u003c/code\u003e error.\u003c/p\u003e\n"]]],[],null,["# Running a custom build step\n\nBy default, the node.js runtime will execute `npm run build` if a `build` script\nis detected in `package.json` file. If you require additional control over your build\nsteps before starting your application, you can provide a custom build\nstep. Custom build steps can be executed by adding `gcp-build` in your\n`package.json` file.\n\nTo prevent your build from running the `npm run build` script, you must either:\n\n- Add a `gcp-build` script with an empty value in your `package.json` file: `\"gcp-build\":\"\"`. For details about configuring the `package.json`, see [Node.js buildpacks configurations](/appengine/docs/standard/nodejs/running-custom-build-step#example).\n- Add the [`GOOGLE_NODE_RUN_SCRIPTS`](/docs/buildpacks/nodejs#google_node_run_scripts)\n build environment variable with an empty value in your `app.yaml` file.\n\n build_env_variables:\n GOOGLE_NODE_RUN_SCRIPTS: ''\n\nFor details about specifying build environment variables see [`build_env_variables`](/appengine/docs/standard/reference/app-yaml#runtime_and_app_elements) section in the `app.yaml` file.\n\nWhen this script is executed, the dependencies in the `dependencies` and\n`devDependencies` fields of your `package.json` file are available.\nAfter executing your custom build step, App Engine removes and regenerates the\n`node_modules` folder by only installing the production dependencies declared\nin the `dependencies` field of your `package.json` file.\n\nFor more information on how to configure the runtime for custom build steps,\nsee [Executing custom build steps during deployment](/docs/buildpacks/nodejs#executing_custom_build_steps_during_deployment)\nfor Node.js buildpacks.\n\nExample\n-------\n\nA custom build script can be used for pre-processing tasks, such as\npre-processing CSS, minifying client side JavaScript, or running tools, such as\n`webpack` or `gulp`.\n\nFor example, to compile TypeScript to JavaScript, your `package.json` file might\nlook like the following. Note the `gcp-build` script: \n\n {\n \"name\": \"appengine-typescript\",\n \"description\": \"An example TypeScript app running on Google App Engine.\",\n \"version\": \"0.0.1\",\n \"private\": true,\n \"license\": \"Apache Version 2.0\",\n \"author\": \"Google Inc.\",\n \"engines\": {\n \"node\": \"20.x\"\n },\n \"scripts\": {\n \"prepare\": \"npm run gcp-build\",\n \"pretest\": \"npm run gcp-build\",\n \"test\": \"c8 mocha -p -j 2 test/*.test.js --exit\",\n \"lint\": \"gts lint\",\n \"start\": \"node ./index.js\",\n \"deploy\": \"gcloud app deploy\",\n \"clean\": \"gts clean\",\n \"compile\": \"tsc -p .\",\n \"fix\": \"gts fix\",\n \"build\": \"tsc -p .\",\n \"gcp-build\": \"tsc -p .\"\n },\n \"dependencies\": {\n \"@typescript-eslint/parser\": \"^8.0.0\",\n \"express\": \"^4.16.3\"\n },\n \"devDependencies\": {\n \"@types/express\": \"^4.17.17\",\n \"@types/node\": \"^20.0.0\",\n \"c8\": \"^10.0.0\",\n \"chai\": \"^4.5.0\",\n \"gts\": \"^5.0.0\",\n \"mocha\": \"^10.2.0\",\n \"typescript\": \"^5.0.0\",\n \"wait-port\": \"^1.0.0\"\n }\n }\n\nServing static files\n--------------------\n\nWhen you generate or copy files during the custom build step, App Engine does\nnot serve these static files directly from your app using either the\n[`static_dir`](/appengine/docs/standard/reference/app-yaml#handlers_static_dir)\nor [`static_files`](/appengine/docs/standard/reference/app-yaml#handlers_static_files)\nelements defined in the `app.yaml` file. Instead, App Engine uploads\nthe static files to the serving infrastructure in the standard environment\nbefore running the custom build step. Any request to the files generated or\ncopied during the custom build step results in a `404 NOT FOUND`\nHTTP error."]]