Search code examples
phpamazon-web-servicessymfonyamazon-elastic-beanstalk

Symfony 6 AWS Beanstalk run npm run build


I'm new to AWS and I've gotten as far as getting the following error in Symfony:

Asset manifest file "/var/app/current/public/build/manifest.json" does not exist.

In local, this would be fixed by running npm run build. I've tried adding NPM_CONFIG_PRODUCTION=true in the environment variables, but I think that might just be for node.js apps?

I've also tried SSHing onto the EC2 instance and installing node on there, but I ran into errors trying to install either npm or nvm. I feel like this is the wrong approach anyway, since it seems like the idea of beanstalk is that you shouldn't need to ssh onto the instance.

Perhaps I should just include the node_modules folder in the zip uploaded, but since one of the recommended ways to produce the zip is to use git, this doesn't seem correct either.


Solution

  • After a lot of digging around, it seems like there's 3 options here:

    1. SSH onto the instance(s) and the following worked for me (Amazon Linux 2 - ARM chip)
    curl --silent --location https://rpm.nodesource.com/setup_16.x | sudo bash -
    sudo yum -y install nodejs
    (cd /var/app/current/;sudo npm add --dev @symfony/webpack-encore)
    (cd /var/app/current/;sudo npm install)
    (cd /var/app/current/;sudo npm run build)
    

    The problem with this, is if you have multiple instances that scale up and down with a load balancer, it isn't really practical to do this.

    1. Add the above as a hook:

    The following sh file could be put in the following directory: .platform/hooks/predeploy

    #!/bin/bash
    curl --silent --location https://rpm.nodesource.com/setup_16.x | sudo bash -
    sudo yum -y install nodejs
    (cd /var/app/current/;sudo npm add --dev @symfony/webpack-encore)
    (cd /var/app/current/;sudo npm install)
    (cd /var/app/current/;sudo npm run build)
    

    However, I've since learnt that it's best advised to just include the node_modules in the zip that gets uploaded. I guess this way the time to get the server up is reduced.

    1. Include the node_modules folder in the zip that gets uploaded. To include the node_modules folder, since this is naturally ignored by GIT, I used the EB CLI and added a .ebignore file, which is a clone of the .gitignore file, but includes the node_modules and public folders. Also be cautious in your build process that you're not including the node dev dependencies.