Install node and npm via nvm on AWS Elastic Beanstalk

Yasen Slavov
2 min readFeb 5, 2018

--

AWS is a platform of choice for small and large companies alike. It’s full featured, easy to automate and generally takes the pain out of the infrastructure side of things.

Of course, there is a “however”, however. Once in a while there is a small quirk that just takes a whole day to figure out, ending with a slap on the forehead and a “well, duuuh!”. Today was one of those days for us at BetaPeak.

We tend to use Elastic Beanstalk A LOT. It just works. We usually deploy it with ECS (AWS’s container service, using Docker), and that is a breeze. However, once in a while we have a smaller project that just doesn’t require all the heavy setup of container orchestration and today we found out that installing older versions of node is not that straight-forward.

So we decided to share our experience with the developer community, hoping we can save you some bang-my-head-against-the-wall time. Here’s a working .ebextensions config:

# your_project_root/.ebextensions/01_install_node_and_compile_source.config
files:
"/opt/elasticbeanstalk/hooks/appdeploy/post/99_node.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash

cd /var/app/current

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash

# Source nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion

# Install node
nvm install 8.6.0

npm i && npm run dev

container_commands:
....

Things to notice:

  • We’re creating a file in the post deployment hook. This hook runs all files on the directory /opt/elasticbeanstalk/hooks/appdeploy/post/, in alphabetical order, after all the app files have been deployed to their final destination in the deployment container.
  • The file will be created with execute permissions because the mode is 000755.
  • The .sh file installs nvm, sources it (this is important!) and then uses it to install a node version of our choice (in this case 8.6.0). Note that you need to then use npm from within that file, as we’ve used it to install our packages and compile our js source code. If you try to install the packages and compile in the container_commands section of the config, you’d get a “npm not found” error.

We hope we’ve saved you some time.

Interested in working with BetaPeak to help build, scale or maintain your startup’s web & mobile apps? Check out what we have to offer at BetaPeak.

--

--

Yasen Slavov

Founder of BetaPeak (https://betapeak.com), a software engineering agency, focused on helping seed and early stage startups get their products off the ground.