LATEST VERSION: 1.9 - CHANGELOG
Pivotal Cloud Foundry v1.9

Tips for Node.js Applications

Page last updated:

This topic provides Node-specific information to supplement the general guidelines in the Deploy an Application topic.

Application Package File

Cloud Foundry expects a package.json in your Node.js app. You can specify the version of Node.js you want to use in the engine node of your package.json file.

In general, Cloud Foundry supports the two most recent versions of Node.js. See the GitHub Node.js buildpack page for current information.

Example package.json file:

{
  "name": "first",
  "version": "0.0.1",
  "author": "Demo",
  "dependencies": {
    "express": "3.4.8",
    "consolidate": "0.10.0",
    "express": "3.4.8",
    "swig": "1.3.2"
  },
  "engines": {
    "node": "0.12.7",
    "npm": "2.7.4"
  }
}

Application Port

You must use the PORT environment variable to determine which port your app should listen on. In order to also run your app locally, you may want to make port 3000 the default:

app.listen(process.env.PORT || 3000);

Application Start Command

Node.js apps require a start command. You can specify the web start command for a Node.js app in a Procfile or in the app deployment manifest. For more information about Procfiles, see the Configuring a Production Server topic.

The first time you deploy, you are asked if you want to save your configuration. This saves a manifest.yml in your app with the settings you entered during the initial push. Edit the manifest.yml file and create a start command as follows:

---
applications:
- name: my-app
  command: node my-app.js
... the rest of your settings ...

Alternately, specify the start command with cf push -c.

$ cf push my-app -c "node my-app.js"

Application Bundling

You do not need to run npm install before deploying your app. Cloud Foundry runs it for you when your app is pushed. If you prefer to run npm install and create a node_modules folder inside of your app, this is also supported.

Solve Discovery Problems

If Cloud Foundry does not automatically detect that your app is a Node.js app, you can override the auto-detection by specifying the Node.js buildpack.

Add the buildpack into your manifest.yml and re-run cf push with your manifest:

---
applications:
- name: my-app
  buildpack: https://github.com/cloudfoundry/nodejs-buildpack
... the rest of your settings ...

Alternately, specify the buildpack on the command line with cf push -b:

$ cf push my-app -b https://github.com/cloudfoundry/nodejs-buildpack

Bind Services

Refer to Configure Service Connections for Node.js.

About the Node.js Buildpack

For information about using and extending the Node.js buildpack in Cloud Foundry, see the nodejs-buildpack repo.

You can find current information about this buildpack on the Node.js buildpack release page in GitHub.

The buildpack uses a default Node.js version. To specify the versions of Node.js and npm an app requires, edit the app’s package.json, as described in “node.js and npm versions” in the nodejs-buildpack repo.

Environment Variables

You can access environments variable programmatically.

For example, you can obtain VCAP_SERVICES like this:

process.env.VCAP_SERVICES

Environment variables available to you include both those defined by the system and those defined by the Node.js buildpack, as described below.

BUILD_DIR

Directory into which Node.js is copied each time a Node.js app is run.

CACHE_DIR

Directory that Node.js uses for caching.

PATH

The system path used by Node.js.

PATH=/home/vcap/app/bin:/home/vcap/app/node_modules/.bin:/bin:/usr/bin

Was this helpful?
What can we do to improve?
View the source for this page in GitHub