Customize AWS ElasticBeanstalk NodeJS Install use yarn
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Elastic Beanstalk’s Node.js platform historically defaults to npm-oriented behavior, but many teams want Yarn for deterministic installs and lockfile alignment. The practical solution is to make the deployment environment install dependencies with Yarn explicitly, using platform hooks or configuration files that match the current Beanstalk platform generation.
Start with a Clear Yarn-Based Project Layout
If the application is supposed to use Yarn, the repository should reflect that clearly:
- '
package.json' - '
yarn.lock' - no conflicting package-manager assumptions in deployment scripts
At minimum, local and CI installs should already succeed with:
If the app still depends on npm conventions locally, fixing Elastic Beanstalk first is the wrong layer.
Use Platform Hooks on Modern Elastic Beanstalk
On Amazon Linux 2 based Node.js platforms, .platform/hooks is the cleanest way to customize install behavior. For example, you can run Yarn during the prebuild phase.
Save that as:
Make it executable:
This approach works because Beanstalk executes the hook as part of the deployment lifecycle before the app is finalized.
Older Platforms Often Use .ebextensions
If you are on an older Beanstalk platform generation, .ebextensions may still be the customization path. A common pattern is to install Yarn and then run it through container_commands.
Put that in a file such as:
The exact mechanism depends on the platform version, which is why checking the current Beanstalk documentation for your stack version matters.
Keep the Build and Start Commands Consistent
Using Yarn for dependency installation but leaving the rest of the platform confused about startup scripts leads to brittle deployments. Make the application lifecycle explicit in package.json.
Then ensure your hook or config runs the right commands:
That keeps Elastic Beanstalk from silently falling back to an npm-oriented assumption later in the deploy.
Treat yarn.lock as the Source of Truth
The whole reason to use Yarn in deployment is usually reproducibility. That only works if the deployment actually respects the lockfile.
Use:
instead of plain yarn install when you want deploys to fail on dependency drift rather than silently rewrite the dependency graph.
If the repo still carries package-lock.json and yarn.lock, choose one package manager and remove the conflicting lockfile. Mixed lockfiles make production dependency behavior harder to predict.
Common Pitfalls
- Installing Yarn but still letting the platform perform the real dependency installation with npm defeats the point of the customization.
- Keeping both
package-lock.jsonandyarn.lockcreates ambiguity about which dependency graph is authoritative. - Using
yarn installwithout--frozen-lockfileallows drift between local, CI, and Beanstalk installs. - Applying
.ebextensionsinstructions to a modern platform that expects.platform/hooksleads to configuration that looks correct but is never executed. - Fixing Beanstalk first when the application itself is not consistently Yarn-based locally makes the deployment symptoms misleading.
Summary
- Make the project consistently Yarn-based before customizing Elastic Beanstalk.
- Use
.platform/hookson modern Node.js Beanstalk platforms and.ebextensionsonly where the platform still expects it. - Install dependencies with
yarn install --frozen-lockfileso the lockfile stays authoritative. - Keep build and start commands explicit and package-manager consistent.
- Verify the hook mechanism against the actual Elastic Beanstalk platform generation you are deploying.

