In EAS Build, what do the eas-build-pre-install and eas-build-on-success scripts in package.json do, and when does each run?
answer
- npm scripts with reserved names
- pre-install: before dependencies exist
- post-install after install and prebuild
- on-success, on-error, on-complete, on-cancel
- EAS_BUILD_PLATFORM to fork per platform
basics
~10 sThey are package.json scripts EAS Build runs at fixed points: eas-build-pre-install before the JavaScript dependencies are installed, and eas-build-on-success at the end of a build that succeeded. Custom builds do not run them automatically.
solid answer
~40 sEAS Build recognises six reserved `package.json` script names. `eas-build-pre-install` runs **before** the package manager installs dependencies, so it can prepare the machine (install a system tool, create a file the install needs) but cannot use anything from `node_modules`. `eas-build-post-install` runs after the install and, when needed, `npx expo prebuild` (plus `pod install` on iOS). At the end, `eas-build-on-success` runs only if the build succeeded, `eas-build-on-error` only if it failed, `eas-build-on-complete` either way with `EAS_BUILD_STATUS` set to `finished` or `errored`, and `eas-build-on-cancel` if it was cancelled. Scripts fork per platform with `EAS_BUILD_PLATFORM`. In custom builds defined in YAML the hooks are not run automatically; your steps must call them.
code
json · 7 lines{
"scripts": {
"eas-build-pre-install": "./scripts/eas-pre-install.sh",
"eas-build-on-success": "node scripts/record-build.js",
"start": "expo start"
}
}go deeper
Recall that EAS Build runs package.json scripts with reserved names, such as eas-build-pre-install and eas-build-on-success, at fixed build stages.
Explain where each hook falls relative to the dependency install and prebuild, and how scripts branch with EAS_BUILD_PLATFORM and EAS_BUILD_STATUS.
Choose the right hook for a job, keep pre-install free of project dependencies, and know hooks do not run in custom builds.
Decide when hooks stop being enough and a custom build or a workflow step should own the logic, for clarity and reviewability.
## What the hooks are **EAS Build lifecycle hooks** are ordinary `package.json` scripts with reserved names. EAS Build looks for them and runs them at fixed points of the build, so you can adapt a managed build without writing a whole custom build definition. | Hook | When it runs | |---|---| | `eas-build-pre-install` | before EAS Build installs the JavaScript dependencies | | `eas-build-post-install` | Android: after the install and `npx expo prebuild` (if needed); iOS: also after `pod install` | | `eas-build-on-success` | at the end, only if the build succeeded | | `eas-build-on-error` | at the end, only if the build failed | | `eas-build-on-complete` | at the end in either case; `EAS_BUILD_STATUS` is `finished` or `errored` | | `eas-build-on-cancel` | if the build is cancelled | ## eas-build-pre-install This is the earliest point you control. Dependencies are **not installed yet**, so the script cannot import project packages; it runs with the tools the build image provides. Typical uses: - installing a system tool the install needs, such as `git-lfs` for a pod fetched through LFS; - generating a file the install or prebuild expects, from values in the build's environment; - adjusting package manager configuration before the install. ## eas-build-post-install At this point the dependencies are installed and, for projects that generate native directories, prebuild has run. It is the place for work that needs both `node_modules` and the native project, such as patching a generated native file. ## eas-build-on-success and its siblings The end-of-build hooks react to the outcome: `on-success` for work that makes sense only with a good artifact, `on-error` for failure-only diagnostics, `on-complete` when one script should handle both and branch on `EAS_BUILD_STATUS`, and `on-cancel` for cleanup after cancellation. Choosing the narrowest one keeps scripts simple. ## Forking per platform One hook serves both platforms, so scripts branch on built-in variables: 1. `EAS_BUILD_PLATFORM` — `android` or `ios`. 2. `EAS_BUILD_PROFILE` — the build profile name, such as `production`. 3. `EAS_BUILD_RUNNER` — `eas-build` in the cloud, `local-build-plugin` under `eas build --local`. 4. `EAS_BUILD=true` and `CI=1` — mark an EAS Build environment. These variables exist on the build machine only; they are not set when app config is evaluated on your laptop. ## Where hooks do not run - **Custom builds** — when a build is defined as YAML steps, the lifecycle hooks are not executed automatically; your steps must call them. - **Your laptop** — `npm install` does not trigger `eas-build-pre-install`; only EAS Build knows the names. ## Hook, custom build or workflow step? Hooks are the lightest way to adapt a build, but they are not the only one: - A **hook** suits a small adjustment inside an otherwise standard build, kept with the code in `package.json`. - A **custom build**, written as YAML steps, suits a build whose sequence itself changes, such as extra steps between prebuild and compilation. Its hooks must then be called explicitly. - An **EAS Workflows** job suits work that belongs before or after the build as a whole, such as tests before it or a notification after it, where the step is visible in the pipeline rather than hidden in a script. A hook that grows into a long script is usually a sign the logic wants one of the other two homes. ## Hooks and the cache field Hooks often sit next to the build profile's `cache` setting. `cache.paths` lists files or directories EAS saves after a **successful** build and restores on later builds **after the JavaScript dependencies are installed**; restoring never overwrites files that already exist. Changing `cache.key` invalidates the cache, and so does changing any other property of the `cache` object; `cache.disabled` switches it off. A hook that generates a file does not need to cache it, while slow-to-fetch inputs, such as a `Podfile.lock` you want to pin, are what `cache.paths` is for.
- Why can a pre-install hook written as node scripts/setup.js fail when it imports a project dependency?`eas-build-pre-install` runs before EAS Build installs dependencies, so `node_modules` does not exist yet. Only Node's built-in modules and the image's tools are available. Move work that needs project packages to `eas-build-post-install`.
- When is a file listed in an EAS build profile's cache.paths saved and restored?It is saved to persistent storage after a successful build and restored on later builds after the JavaScript dependencies are installed, without overwriting files already present. Changing `cache.key` or any other `cache` property invalidates it, and local builds do not use it.
saying these in an interview costs you the question
- eas-build-pre-install can import packages from node_modules
- eas-build-on-success also runs when the build fails
- Lifecycle hooks run automatically inside custom YAML builds
- npm install on a laptop triggers eas-build-pre-install
- EAS_BUILD_PLATFORM is available when app config is evaluated locally