Including Modules

Set up your dependencies


Webtask allows you to use many of the available npm modules. The simplest way to add npm modules to your code is via the editor but the CLI provides support as well.

Using the wt-cli

The CLI provides two main ways of adding dependencies to a webtask. The first requires using a package.json. This will allow you to include almost any module that has been published to the npm registry.

If you don't already have one, create a package.json file similar to the below example. Put the file in the same directory that your webtask.js file is in (or will be created in).

 "dependencies": {
   "lodash": "4.x"

Note that Webtask will read dependencies from the "dependencies" only, and not from the dev dependencies or others. After your package.json file is created (or if you already had one), create a Webtask (or edit an existing one) next to the package.json file that will read dependencies from it, similarly to the below example.

const _ = require('lodash');
module.exports = (ctx, cb) => {
  // Respond with supported lodash methods
  cb(null, Object.keys(_));

Now, when you create or update your task using the wt-cli, since you have a package.json file adjacent to the webtask file, the CLI will automatically add those dependencies to the platform, and create your Webtask with the resolved versions of those dependencies attached to it.

As an alternative, you can also use the --dependency flag. By adding --dependency NAME@VERSION to you your CLI create execution, you will add the npm module specified by NAME at the specific version as supplied by VERSION.

If you use this option and also have an existing package.json file, you may want to add --ignore-package-json to ensure it is not checked for dependancies.

How Webtask dependencies work

When the wt-cli detects a package.json adjacent to your Webtask file, several things will happen:

  1. Each dependency in your dependencies will be resolved to determine the best matching version available from the npm registry.
  2. The Webtask cluster will then install any modules that have not previously been installed. If all dependencies are already available, this will be immediate. If there are any issues with the requested dependencies, the creation of the task will fail.
  3. Once all of the dependencies are available, they will be encoded in the wt-node-dependencies metadata property of the Webtask that is being created (or updated).
  4. The Webtask is now created!

Note: The wt-node-dependencies metadata property must contain a JSON encoded object, mapping the packages' names to their absolute versions. In the above example, this would be represented by a wt-node-dependencies metadata property whose value is {"lodash":"4.17.4"} (the latest release of lodash at the time of this writing).