Esecuzione di un passaggio di compilazione personalizzato
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Per impostazione predefinita, il runtime Node.js eseguirà npm run build se viene rilevato uno script build
nel file package.json. Se hai bisogno di un maggiore controllo sui passaggi di build
prima di avviare l'applicazione, puoi fornire un passaggio di build
personalizzato. I passaggi di build personalizzati possono essere eseguiti aggiungendo gcp-build nel file
package.json.
Per impedire l'esecuzione dello script npm run build nella build, devi:
Aggiungi uno script gcp-build con un valore vuoto nel file package.json:
"gcp-build":"". Per informazioni dettagliate sulla configurazione di package.json, vedi
Configurazioni dei buildpack Node.js.
Aggiungi la variabile di ambiente di build GOOGLE_NODE_RUN_SCRIPTS
con un valore vuoto nel file app.yaml.
build_env_variables:GOOGLE_NODE_RUN_SCRIPTS:''
Per informazioni dettagliate su come specificare le variabili di ambiente di build, vedi la sezione
build_env_variables nel file app.yaml.
Quando questo script viene eseguito, le dipendenze nei campi dependencies e
devDependencies del file package.json sono disponibili.
Dopo l'esecuzione del passaggio di build personalizzato, App Engine rimuove e rigenera la cartella node_modules installando solo le dipendenze di produzione dichiarate nel campo dependencies del file package.json.
Uno script di build personalizzato può essere utilizzato per attività di pre-elaborazione, ad esempio
la pre-elaborazione di CSS, la minimizzazione di JavaScript lato client o l'esecuzione di strumenti come
webpack o gulp.
Ad esempio, per compilare TypeScript in JavaScript, il file package.json potrebbe
avere il seguente aspetto. Nota lo script 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"}}
Gestione dei file statici
Quando generi o copi file durante il passaggio di compilazione personalizzata, App Engine non gestisce questi file statici direttamente dalla tua app utilizzando gli elementi
static_dir
o static_files
definiti nel file app.yaml. App Engine carica
i file statici nell'infrastruttura di pubblicazione nell'ambiente standard
prima di eseguire il passaggio di compilazione personalizzato. Qualsiasi richiesta ai file generati o
copiati durante il passaggio di build personalizzata genera un errore HTTP 404 NOT FOUND.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 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."]]