在Elastic Beanstalk上更改Node和NPM版本

时间:2019-01-26 08:50:02

标签: ruby-on-rails node.js amazon-web-services amazon-elastic-beanstalk

我的Rails应用程序上的红宝石需要特定的节点和NPM版本。我正在使用的Elastic Beanstalk平台是带乘客的ruby 2.3。它已经预部署了node和npm,但是版本不是我所需要的。

[root@ip-172-31-35-134 configuration]# /opt/elasticbeanstalk/support/node-install/node-v4.6.0-linux-x64/bin/node -v
v4.6.0
[ec2-user@ip-172-31-35-134 ~]$ /opt/elasticbeanstalk/support/node-install/node-v4.6.0-linux-x64/bin/npm -v
2.15.9

任何想法如何更改默认vesions?

1 个答案:

答案 0 :(得分:0)

将其放入.ebextensions

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/10_nodejs.sh":
    mode: "000777"
    owner: 'root'
    group: 'root'
    content: |
        #!/usr/bin/env bash

        . /opt/elasticbeanstalk/hooks/common.sh

        set -xe
        EB_NODE_VERSION="v8.9.4"

        file="/etc/elasticbeanstalk/baking_manifest/node_installed_$EB_NODE_VERSION"
        if [ -e $file ]; then
            echo Node.js has already been installed. Skipping installation.
        else
            EB_TARBALL_URL=$(/opt/elasticbeanstalk/bin/get-config container -k tarball_url)
            EB_NODE_INSTALL_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k node_install_dir)

            MACHINE_TYPE=`uname -m`
            if [ "$MACHINE_TYPE" = "x86_64" ]; then
                ARCH=x64
            elif [ "$MACHINE_TYPE" = "i686" ]; then
                ARCH=x86
            else
                echo "Unknown architecture."
                exit 1
            fi

            mkdir -p $EB_NODE_INSTALL_DIR
            curl -L https://nodejs.org/dist/$EB_NODE_VERSION/node-$EB_NODE_VERSION-linux-$ARCH.tar.gz | tar zxf - -C $EB_NODE_INSTALL_DIR
            ln -sf $EB_NODE_INSTALL_DIR/node-$EB_NODE_VERSION-linux-$ARCH/bin/node /usr/bin/
            ln -sf $EB_NODE_INSTALL_DIR/node-$EB_NODE_VERSION-linux-$ARCH/bin/node-waf /usr/bin/
            ln -sf $EB_NODE_INSTALL_DIR/node-$EB_NODE_VERSION-linux-$ARCH/bin/npm /usr/bin/
            echo $(date) >> "/etc/elasticbeanstalk/baking_manifest/node_installed_$EB_NODE_VERSION"

            rm -rf /home/webapp/.npm/
            rm -rf /var/app/ondeck/node_modules/
            npm cache clean --force
            rm -rf /opt/elasticbeanstalk/support/node-install/node-v6*
            rm -rf /opt/elasticbeanstalk/support/node-install/node-v4*
        fi

        mkdir -p /home/webapp
        chown -R webapp:webapp /home/webapp
        mkdir -p /var/app/support/.npm_global
        npm config set prefix /var/app/support/.npm_global