title | description | author | tags | date_published |
---|---|---|---|---|
Webpack on App Engine Flexible Environment |
Learn how to bundle frontend assets for an Express.js app in the Google App Engine flexible environment. |
jmdobry |
App Engine, Express.js, Node.js, Webpack |
2017-02-16 |
This tutorial shows a sample Node.js app built with Express.js that uses Webpack to bundle frontend assets on deployment to the Google App Engine flexible environment.
Webpack is a module bundler. Webpack takes modules with dependencies and generates static assets representing those modules.
This tutorial gets you going fast by deploying a simple Webpack.js app. This tutorial assumes that you are familiar with Node.js programming and that you have installed Node.js.
You can check out Node.js and Google Cloud Platform to get an overview of Node.js itself and learn ways to run Node.js apps on Google Cloud Platform.
- Create a Node.js app that uses Webpack to bundle the app's frontend assets.
- Run the Node.js app locally.
- Deploy the Node.js app to Google App Engine flexible environment.
This tutorial uses billable components of Google Cloud Platform, including:
- Google App Engine flexible environment
Use the Pricing Calculator to generate a cost estimate based on your projected usage.
- Create a project in the Google Cloud Platform Console.
- Enable billing for your project.
- Install the Google Cloud SDK.
-
Download the sample
server.js
file. -
Prepare the
package.json
file:-
Create a
package.json
file withnpm
oryarn
:npm init
or
yarn init
-
Install dependencies with
npm
oryarn
:npm install --save webpack express pug
or
yarn add webpack express pug
Note: Webpack must be listed in the
dependencies
of thepackage.json
file, as by defaultdevDependencies
are not installed when the app is deployed to Google App Engine. -
Add the following
scripts
section to thepackage.json
file:"scripts": { "bundle": "webpack --config webpack.config.js", "prestart": "npm run bundle" }
- The
bundle
script will run Webpack and bundle the app's frontend assets. - The
prestart
script is automatically executed before thestart
script. Note that the defaultstart
script isnode server.js
.
Make sure that your
package.json
file is valid JSON. - The
-
-
Create a
webpack.config.js
file with the following contents:'use strict'; module.exports = { entry: './public/app.js', output: { filename: './dist/bundle.js' } };
The bundled assets will be places in a directory named
dist
. -
Prepare the frontend assets:
-
Create a new directory named
public
:mkdir public
-
Create a file in the
public
directory namedapp.js
with the following contents:'use strict'; import foo from './foo'; document.getElementById('module-name').innerText = foo.name;
-
Create a file in the
public
directory namedfoo.js
with the following contents:'use strict'; export default { name: 'foo' };
app.js
imports and usesfoo.js
, but these files will be bundled and deployed as one file. -
-
Prepare the app's views:
-
Create a new directory named
views
:mkdir views
-
Download the sample
index.pug
file into theviews
directory.
-
At this point your directory structure should look like this:
public/
app.js
foo.js
views/
index.pug
package.json
server.js
webpack.config.js
-
Start the app with
npm
oryarn
:npm start
or
yarn start
The
prestart
andbundle
scripts will be executed automatically, bundling the frontend assets before the app starts. -
Visit http://localhost:8080 to see the running app.
-
Press Control+C to stop the app.
-
Create an
app.yaml
file with the following contents:runtime: nodejs env: flex
-
Run the following command to deploy your app:
gcloud app deploy
-
Visit
http://[YOUR_PROJECT_ID].appspot.com
to see the deployed app.Replace
[YOUR_PROJECT_ID]
with your Google Cloud Platform project ID.