If you are referring to elastic beanstalk nodejs ec2, then this answer is best for you, as it took me a while to figure this out, but it turns out to be easier than I thought:
- Following this link with some modifications I did to avoid
/usr/bin/env: node: No such file or directory
issues, I added the following script
.ebextensions/angular2deployment.config
files:
"/opt/elasticbeanstalk/env.vars" :
mode: "000775"
owner: root
group: users
content: |
export NPM_CONFIG_LOGLEVEL=error
export NODE_PATH=`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin
"/opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh" :
mode: "000775"
owner: root
group: users
content: |
#!/bin/bash
. /opt/elasticbeanstalk/env.vars
function error_exit
{
eventHelper.py --msg "$1" --severity ERROR
exit $2
}
#install not-installed yet app node_modules
if [ ! -d "/var/node_modules" ]; then
mkdir /var/node_modules ;
fi
if [ -d /tmp/deployment/application ]; then
ln -s /var/node_modules /tmp/deployment/application/
fi
OUT=$([ -d "/tmp/deployment/application" ] && cd /tmp/deployment/application && $NODE_PATH/node $NODE_PATH/npm install 2>&1) || error_exit "Failed to run npm install. $OUT" $?
echo $OUT
"/opt/elasticbeanstalk/hooks/configdeploy/pre/50npm.sh" :
mode: "000666"
owner: root
group: users
content: |
#no need to run npm install during configdeploy
- Delete
node_modules
& dist
folder if you have, both are not needed.
- Run
npm install && npm start
(this step must be successful), note that I'm using the angular2's default package.json
See Angular.IO Deployment
- If #3 is successful, then you can re-delete
node_modules
again
- Select All files & folders in the project (make sure
.ebextensions
is selected as well), and then compress them, do not compress the top folder (you will have subdirectory when deploying which will break the deployment)
- Now you can deploy that compressed file and enjoy!
If you are using MacOS, while compressing, macos will add macos folder which will break the deployment, make sure using a tool that won't add this extra directory, in my case I used YemuZip
Additional note: EC2 elastic beanstalk will run npm install
& npm start
, this is why I would recommend running them and make sure that they are fine on your local environment