How do I install an NPM package for my CloudCode task?

There is a use case where I would like to import Lodash to group data and filter arrays. How do I install a package from NPM?

The official documentation is here:

JourneyApps Docs

Dependencies (Advanced)

Defaults

A few npm packages are included by default in every task. This includes:

-aws-sdk
-handlebars
-node-fetch (exposed as a fetch global function)

Additional npm dependencies can be added to a task, but this process currently requires using offline tools.

Requirements

-NodeJS (6.10.0 or later)
-Yarn
-The task must use CloudCode version 0.3.0 or later.

Step 1: Add the dependency

Setup a node project for the task:

mkdir mytask
cd mytask
yarn init . # The default options are fine for this
Add the required dependency, e.g. rollbar:

yarn add rollbar In the editor, edit your package.json file. Edit the task, then in the URL bar, replace mytask/index.js with mytask/package.json.

Replace the dependencies (and devDependencies if present) sections in the CloudCode task with the ones from the local project.

The package.json should now look something like this:

{
    "name": "mytask",
    "cloudcode": {
        "runtime": "1.0.0",
        "enabled": true
    },
    "dependencies": {
        "rollbar": "^2.2.7"
    }
}

Edit the yarn.lock file for the task. Same process as above, but replace mytask/index.js with mytask/yarn.lock.

Paste the contents of the entire local yarn.lock file there, e.g.

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
async@~1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/async/-/async-1.2.1.tgz#a4816a17cd5ff516dfa2c7698a453369b9790de0"

console-polyfill@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/console-polyfill/-/console-polyfill-0.3.0.tgz#84900902a18c47a5eba932be75fa44d23e8af861"

debug@2.6.8:
version "2.6.8"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc"
dependencies:
ms "2.0.0"

decache@^3.0.5:
version "3.1.0"
resolved "https://registry.yarnpkg.com/decache/-/decache-3.1.0.tgz#4f5036fbd6581fcc97237ac3954a244b9536c2da"
dependencies:
find "^0.2.4"

error-stack-parser@1.3.3:
version "1.3.3"
resolved "https://registry.yarnpkg.com/error-stack-parser/-/error-stack-parser-1.3.3.tgz#fada6e3a9cd2b0e080e6d6fc751418649734f35c"
dependencies:
stackframe "^0.3.1"

extend@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.0.tgz#5a474353b9f3353ddd8176dfd37b91c83a46f1d4"

find@^0.2.4:
version "0.2.7"
resolved "https://registry.yarnpkg.com/find/-/find-0.2.7.tgz#7afbd00f8f08c5b622f97cda6f714173d547bb3f"
dependencies:
traverse-chain "~0.1.0"

is_js@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/is_js/-/is_js-0.9.0.tgz#0ab94540502ba7afa24c856aa985561669e9c52d"

json-stringify-safe@~5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"

lru-cache@~2.2.1:
version "2.2.4"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-2.2.4.tgz#6c658619becf14031d0d0b594b16042ce4dc063d"

ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

request-ip@~2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/request-ip/-/request-ip-2.0.2.tgz#deeae6d4af21768497db8cd05fa37143f8f1257e"
dependencies:
is_js "^0.9.0"

rollbar:
version "2.2.7"
resolved "https://registry.yarnpkg.com/rollbar/-/rollbar-2.2.7.tgz#516d1f3dd1d5c4679a219e153acb630e8fbe4fa8"
dependencies:
async "~1.2.1"
console-polyfill "0.3.0"
debug "2.6.8"
error-stack-parser "1.3.3"
extend "3.0.0"
json-stringify-safe "~5.0.0"
lru-cache "~2.2.1"
request-ip "~2.0.1"
uuid "3.0.x"
optionalDependencies:
decache "^3.0.5"

stackframe@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/stackframe/-/stackframe-0.3.1.tgz#33aa84f1177a5548c8935533cbfeb3420975f5a4"

traverse-chain@~0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/traverse-chain/-/traverse-chain-0.1.0.tgz#61dbc2d53b69ff6091a12a168fd7d433107e40f1"

uuid@3.0.x:
version "3.0.1"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.0.1.tgz#6544bba2dfda8c1cf17e629a3a305e2bb1fee6c1"

5 Likes

100%. If you also want to use your own private NPM repository, you can create an .npmrc file in the Cloud Code task and include your npm token in there the same way you would include it locally or on a server. This will give the Cloud Code task to retrieve your own private module from NPM.