My lambda function's `package.json` file is causing mystery failures

Hi all,

I’m trying to add some lambda functions to my netlify site, but when I try to load in npm modules (through a package.json file in the functions directory), I get a mystery error.

The site is a static site based on nanoc, so the build is all ruby-based. I’ve got a couple of lambda functions in the functions directory, which is mapped in netlify.toml. I’ve been loading in a couple of npm modules (using David Wells’ tutorials for guidance).

As soon as I add the file functions/package.json to the site, it refuses to build. The log file isn’t particularly helpful - excerpt below:

9:31:56 PM: Build ready to start
9:31:57 PM: build-image version: 8e315e54bc4032a32e73290be556cde4f8348c12
9:31:57 PM: build-image tag: v2.8.2
9:31:57 PM: buildbot version: 7aa30f4805782ff59fecad56e0bfa4b63404839b
9:31:57 PM: Fetching cached dependencies
9:31:57 PM: Starting to download cache of 316.3MB
9:32:00 PM: Finished downloading cache in 2.575764455s
9:32:00 PM: Starting to extract cache
9:32:08 PM: Finished extracting cache in 8.70762282s
9:32:09 PM: Finished fetching cache in 11.8077935s
9:32:09 PM: Starting to prepare the repo for build
9:32:13 PM: Preparing Git Reference refs/heads/staging
9:32:15 PM: Found netlify.toml. Overriding site configuration
9:32:15 PM: Different publish path detected, going to use the one specified in the toml file: 'output' versus 'output/' in the site
9:32:15 PM: Creating functions prep folder
9:32:15 PM: Starting build script
9:32:15 PM: Installing dependencies
9:32:15 PM: Started restoring cached node version
9:32:19 PM: Finished restoring cached node version
9:32:19 PM: v8.17.0 is already installed.
9:32:20 PM: Now using node v8.17.0 (npm v6.13.4)
9:32:20 PM: Required ruby-2.7.0-preview1 is not installed.
9:32:20 PM: To install do: 'rvm install "ruby-2.7.0-preview1"'
9:32:20 PM: Attempting ruby version 2.7.0-preview1, read from .ruby-version file
9:32:21 PM: Started restoring cached ruby version
9:32:21 PM: Finished restoring cached ruby version
9:32:23 PM: Using ruby version 2.7.0-preview1
9:32:23 PM: Using PHP version 5.6
9:32:23 PM: Started restoring cached ruby gems
9:32:23 PM: Finished restoring cached ruby gems
9:32:23 PM: Started restoring cached go cache
9:32:24 PM: Finished restoring cached go cache
9:32:24 PM: Installing Go version 1.10
9:32:24 PM: unset GOOS;
9:32:24 PM: unset GOARCH;
9:32:24 PM: export GOROOT='/opt/buildhome/.gimme_cache/versions/go1.10.linux.amd64';
9:32:24 PM: export PATH="/opt/buildhome/.gimme_cache/versions/go1.10.linux.amd64/bin:${PATH}";
9:32:24 PM: go version >&2;
9:32:24 PM: export GIMME_ENV='/opt/buildhome/.gimme_cache/env/go1.10.linux.amd64.env';
9:32:24 PM: go version go1.10 linux/amd64
9:32:24 PM: Installing missing commands
9:32:24 PM: Verify run directory
9:32:24 PM: Executing user command: bundle exec nanoc compile --env=prod
9:32:25 PM: Loading site…
9:32:25 PM: done
9:32:28 PM: Compiling site…
9:32:28 PM:       create  [0.40s]  output/_redirects
(...snipping several hundred lines of files being created in nanoc...)
9:32:29 PM:       create  [0.02s]  output/posts/2017/09/11/on-d-d-and-preparation/index.html
9:32:29 PM:       create  [0.01s]  output/posts/2017/11/11/disabling-disqus/index.html
9:32:29 PM:       create  [0.02s]  output/posts/2018/03/26/site-update/index.html
9:32:31 PM: failed during stage 'building site': Build script returned non-zero exit code: 1
9:32:29 PM:       create  [0.01s]  output/posts/2018/05/07/the-case-of-the-missing-zero-index/index.html
(...snipping about 20 more lines of files being created in nanoc...)

So it seems (I’m guessing?) that Netlify is compiling the functions in parallel with the main site, and when it encounters an error it stops the site build. Unfortunately, I don’t get any information on why the build script returned a non-zero code, just that it happened. If I remove or rename functions/package.json, the error disappears. Similarly, I can build the site fine locally (where I’m obviously not doing anything with the functions).

The contents of functions/package.json are:

  "name": "functions",
  "version": "1.0.0",
  "description": "",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "faunadb": "^4.0.0",
    "mailgun-js": "^0.22.0"

I currently have both functions/node_modules and functions/package-lock.json in my .gitignore - I’ve played around a bit with adding and removing them from the gitignore, and it doesn’t seem to make any difference.

So I guess my main questions are:

  1. Is there any obvious thing I’m doing wrong?
  2. If not, are there any ways I can easily debug what’s happening on the netlify end?

Many thanks for your help!

hmm, interesting. usually, build error 1 means that we don’t understand which build command to use.

Could you post your full package.json please? Also a screenshot of your build settings?

Hey perry, thanks for the response! Super weird that Netlify doesn’t know which build command to use.

The full package.json is as quoted above - there’s no package.json in the root folder as the build process doesn’t otherwise rely on javascript.

Build settings are pretty boring:

Screen Shot 2020-12-29 at 11.21.19 AM

Hey @jyr,
If your functions directory has its own package.json, we won’t automatically find it and install dependencies- can you try adding cd functions/ && npm install && cd ../ && bundle exec nanoc compile --env=prod to get into the functions folder, install dependencies, then go back to root and run your main build command?

Hi @jen! Thanks for the heads-up!

I actually got a bit more info on this error by installing the Netlify CLI app (prior to this I’d just been git add/commit/pushing to explore deployment). That gave me a bit more of an insight into what was going wrong.

I definitely wasn’t compiling the functions (as you noted), but I also discovered that one of my functions was requiring a package which I didn’t have in my package.json file (I’d been requiring foo rather than foo-js, silly me). So a rookie mistake on my behalf, but one that netlify deploy pointed out to me front-and-centre. Once I’d fixed that, deployment worked fine.

Many thanks for your help @jen and @perry!

1 Like