Powered By GitBook
install-bower-dependencies-on-google-app-engine
Learn how to use Bower in a Node.js App Engine flexible environment app.
Contributed by Google employees.
If you're using Bower to install web dependencies for your app and you're deploying your app to App Engine flexible environment, then there are several ways to make sure the dependencies are available to your deployed app. This tutorial discusses three different methods.

Prerequisites

    1.
    Create a project in the Cloud Console.
    2.
    Enable billing for your project.
    3.
    Install the Cloud SDK.

Prepare

    1.
    Install Bower:
    1
    npm install -g bower
    Copied!
    2.
    Create a bower.json file if you don't already have one:
    1
    bower init
    Copied!
    3.
    Then save new dependencies to your bower.json file with:
    1
    bower install --save [YOUR_PACKAGE_NAME]
    Copied!

Easiest: Do nothing

When you deploy to App Engine flexible enviroment a Docker image is created for you and your code is copied into the image. This first method relies on the Docker image build step to make Bower dependencies available to your app. This method is the easiest.
This method is simple:
    1.
    Install Bower dependencies locally
    2.
    Deploy
For example:
1
cd my-app
2
bower install
3
gcloud app deploy
Copied!
The Bower dependencies will be installed, and during the deployment the files will be copied into the Docker image, and thus will be available to your deployed app.

Less easy: Use package.json

Let's say you don't want locally installed Bower dependencies to be copied into the Docker image. To make the dependencies available to your deployed app, you can have the dependencies installed inside the Docker image as it is constructed.
    1.
    Run the following command to generate an `app.yaml file if you don't already have one:
    1
    gcloud app gen-config . --runtime=nodejs
    Copied!
    2.
    To prevent locally installed Bower dependencies from being copied into the Docker image, add the following to app.yaml:
    1
    # Prevents locally installed Bower dependencies
    2
    # from being copied into the Docker image
    3
    skip_files:
    4
    - ^(.*/)?.*/bower_components/.*$
    Copied!
    3.
    If you're deploying a Node.js app then you almost certainly have a package.json file. Add the following to package.json:
    1
    "scripts": {
    2
    ...
    3
    "postinstall": "bower install --config.interactive=false",
    4
    ...
    5
    },
    6
    "dependencies": {
    7
    ...
    8
    "bower": "^1.7.9",
    9
    ...
    10
    }
    Copied!
    The ellipses hide other configurations that may exist in package.json.
    4.
    Now deploy:
    1
    gcloud app deploy
    Copied!
    As the Docker image is built it will run npm install, which will in turn run bower install.

Much less easy: Use a Dockerfile

Another method that does not rely on package.json is to use a custom Dockerfile.
To make Docker install the Bower dependencies you need to use runtime: custom. Here we assume you're deploying a Node.js app, but with a little extra customization in the Dockerfile you can make this work for other languages.
    1.
    Run the following command to generate the necessary files:
    1
    gcloud app gen-config . --custom --runtime=nodejs
    Copied!
    This generates three files: Dockerfile, .dockerignore, and app.yaml.
    2.
    To prevent locally installed Bower dependencies from being copied into the Docker image. Add the following to app.yaml:
    1
    # Prevents locally installed Bower dependencies
    2
    # from being copied into the Docker image
    3
    skip_files:
    4
    - ^(.*/)?.*/bower_components/.*$
    Copied!
    3.
    Now edit Dockerfile and insert the following beneath COPY ./app/:
    1
    npm i -g bower
    2
    bower install --config.interactive=false
    Copied!
    So that is has:
    1
    COPY ./app/
    2
    npm i -g bower
    3
    bower install --config.interactive=false
    Copied!
    4.
    Now deploy:
    1
    gcloud app deploy
    Copied!
    As the Docker image is built it will run bower install.
Last modified 6mo ago