Declare App Dependencies

Page last updated:

Page last updated:

Cloud Foundry recognizes an app as Node.js by the existence of a package.json file in the root directory. For your own apps, you can create one by running npm init.

The demo app you deployed already has a package.json, and it looks something like this:

{
  "name": "cf-sample-app-nodejs",
  "description": "A sample Node.js application to run on Cloud Foundry out of the box",
  "keywords": [
    "cloud-foundry"
  ],
  "version": "0.0.0",
  "private": true,
  "author": "Tobias Fuhrimann <tobias.fuhrimann@swisscom.com>",
  "license": "ISC",
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "body-parser": "~1.15.0",
    "cookie-parser": "~1.4.1",
    "debug": "~2.2.0",
    "express": "~4.13.4",
    "morgan": "~1.7.0",
    "pug": "^2.0.0-alpha3",
    "serve-favicon": "~2.3.0"
  },
  "engines": {
    "node": ">=4.4.2"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/swisscom/cf-sample-app-nodejs.git"
  },
  "bugs": {
    "url": "https://github.com/swisscom/cf-sample-app-nodejs/issues"
  }
}

The package.json file determines both the version of Node.js that will be used to run your application on Cloud Foundry, as well as the dependencies that should be installed with your application. When an app is deployed, Cloud Foundry reads this file and installs the appropriate node version together with the dependencies using the npm install command.

Run this command in your local directory to install the dependencies, preparing your system for running the app locally:

$ npm install
cf-sample-app-nodejs@0.0.0 /.../cf-sample-app-nodejs
├─┬ body-parser@1.15.0
│ ├── bytes@2.2.0
│ ├── content-type@1.0.1

...

Once dependencies are installed, you will be ready to run your app locally.

Upon running cf push, we will also push our whole dependencies folder node_modules. Since Cloud Foundry runs npm install anyways, this is redundant and should be omitted to save bandwidth and even more importantly: time. You can create a .cfignore file which works just like a .gitignore file and which tells Cloud Foundry what files should be excluded when pushing. Let’s create a .cfignore file and exclude the node_modules folder:

node_modules

Pro Tip: To lock down your dependencies’ versions before pushing, you can run npm shrinkwrap. This will create a npm-shrinkwrap.json file which holds the exact versions of all your dependencies. Any npm install will then install exactly these versions so you can be sure that your app is using versions which work.

I’ve installed the App dependencies locally
View the source for this page in GitHub