Node.js Builder (@now/node)

The @now/node Builder takes an entrypoint of a Node.js function, builds its dependencies (if any) and bundles them into a Lambda.

This Builder is the recommended way to introduce any Node.js-based dynamic request handling into your codebases.

It can be used directly (as a single file, like my-function.js), or you can define an index.js file inside a directory.

For this example, we will create a cowsay endpoint that we want to access as my-deployment.url/api/cowsay.

Let's create our project structure:

mkdir -p my-project/api/cowsaycd my-project/api/cowsay

Inside myproject > api > cowsay we will create the following files:

  • index.js
  • package.json
  • yarn.lock

To do so, let's run yarn to add our cowsay module dependency:

yarn add cowsay

At this point, it is a good idea to define a .nowignore file that contains:


The index.js file exports a Node.js function that takes in the standard Node.js request and response objects:

const { parse } = require("url");
const cowsay = require("cowsay/build/cowsay.umd.js").say;
module.exports = (req, res) => {
  const { query } = parse(req.url, true);
  const { text = "Use query `text`" } = query;
  res.end(cowsay({ text }));

The only step left is to define a build that will take this directory's entrypoint (index.js), build it and turn it into a lambda:

  "version": 2,
  "builds": [
    { "src": "api/cowsay/index.js", "use": "@now/node" }

Our resulting deployment is here:

You can pass query parameters to make the cow change:

An illustration of the deployment readyState lifecycle.

The entrypoint of this Builder is always a JavaScript file that exposes a function. If you want to expose a server, look into @now/node-server, although that is not recommended.

The installation algorithm of dependencies works as follows:

  • If a package-lock.json is present, npm install is used
  • Otherwise, yarn is used.

To install private npm modules, define NPM_TOKEN as a build environment variable in now.json.

The Node.js version used is the latest in the 8 branch.

To help keep cold boot times low, the maximum output bundle size for a Node.js output lambda is, by default, 5mb. This limit is extendable up to 50mb.

Example maxLambdaSize configuration:
  "builds": [
    { "src": "*.js", "use": "@now/node", "config": { "maxLambdaSize": "10mb" } }