How to run `npm install` in Amazon Elastic Beanstalk with .ebextensions
Asked Answered
E

3

11

I want to deploy my PHP app in a Amazon Elastic Beanstalk with eb deploy command. But my app use gulp to concat and minify scss and js.

So I tried these commands to in the file .ebextensios/03npm.config

commands:
  01-install-node:
    command: "yum install nodejs npm --enablerepo=epel -y"

container_commands:
  01-install-dependencies:
    command: "npm install"
  02-build:
    command: "npm run build"

But in the end I receive this error

[Instance: i-c7800103] Command failed on instance. Return code: 1 Output: (TRUNCATED)...ttps://registry.npmjs.org/acorn npm http 304 https://registry.npmjs.org/amdefine npm http 304 https://registry.npmjs.org/wrappy npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /var/app/ondeck/npm-debug.log npm ERR! not ok code 0. container_command 01-install-dependencies in .ebextensions/03npm.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

I'm not sure but it appears that npm install receive an error from one package that could be ignored but it is dispatching the EB error and stopping the whole process.

I'm running with this machine: 64bit Amazon Linux 2015.09 v2.0.4 running PHP 5.6

Does anyone know how we can fix this?

Everest answered 5/11, 2015 at 19:15 Comment(3)
I'm experiencing same issue w. almost identical .config file in .ebextensions. Did you find a solution, and if so would you care to share? Thanks.Wade
I post my solution in the anwser to explain it betterEverest
I faced the same issue yesterday but didn't like any of the answers, found the cause in the logs. Specifically, you want to look at eb-activity.log and eb-commandprocessor.log in order to get more information about this error. I found mine to be something to do with my npm installation itself. So it wasn't an eb issue itself, I fixed that simple npm error and moved on thereafter. Hope this information helps someone out there =)Adna
F
4

Here is what worked for me:

packages:
    yum:
      git: []

commands:
    01_node_install:
        cwd: /tmp
        test: '[ ! -f /usr/bin/node ] && echo "node not installed"'
        command: 'yum install -y nodejs --enablerepo=epel'
    02_npm_install:
        cwd: /tmp
        test: '[ ! -f /usr/bin/npm ] && echo "npm not installed"'
        command: 'sudo rm -rf /usr/bin/npm | curl -L http://npmjs.org/install.sh | sh'
    03_node_update:
        cwd: /tmp
        test: '[ ! -f /usr/bin/n ] && echo "node not updated"'
        command: 'npm install -g n && n stable'

container_commands:
  # your container commands

UPD: in some cases yum may fail to install nodejs from the repo, as there are rare cases when repo URLs aren't available, or downloads fail. In such a case the whole build will fail, as npm install won't succeed. The solution is to use custom AWS AMIs (Amazon Machine Images) with nodejs pre-installed on them: documentation.

Frenchy answered 24/8, 2020 at 8:7 Comment(2)
This is the only option that worked for me. Thanks!Debera
This comment is definitely a time saver!Meissen
E
1

Until now, I have this ugly solution, but it works. I created this script inside package.json that do the hard job in sequence. It create a new branch and commit the compiled files before deploy it to EB

package.json

{
  "scripts": {
    "production": "node ./node_modules/gulp/bin/gulp.js --production",
    "deploy": "git checkout -b deploy && npm run production && git add . && git commit -m \"build\" && eb deploy && git checkout master && git branch -D deploy"
  }
}

.elasticbeanstalk/config.yml

branch-defaults:
  deploy:
    environment: NAME_OF_ENVIRONMENT
Everest answered 8/1, 2016 at 18:44 Comment(1)
Thanks for this. I actually ended up using Circle CI and Docker (since my experience w. AWS Node.js environments is not the best). By using Circle CI I'm able to deploy directly from Github. I.e. Circle CI takes the latest push to e.g. master, then runs npm install and then builds the project into a distribution folder, which is then passed to EB. Using .ebignore I'm able to ignore everything but the distribution folder and the production server. These are therefore the only files passed on to AWS EB. From there AWS EB creates a Docker container and runs it in ~3-4 mins.Wade
S
0
packages:
  yum:
    git: []

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/03_npm_install.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      cd $EB_APP_STAGING_DIR

      npm install bower
      npm install
      ./node_modules/bower/bin/bower install --allow-root --config.interactive=false
      ./node_modules/grunt-cli/bin/grunt dust
Scandinavian answered 14/11, 2015 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.