Function cannot find modules installed in root package.json when deployed via GitHub

Function works perfectly when:

  1. Running in my local environment using netlify dev
  2. Deployed via netlify deploy
  3. Running a similar setup in other web apps I’ve worked on like https://95-percent.netlify.app/.netlify/functions/questions

Function doesn’t work when: deploying automatically via GitHub
Source repository: https://github.com/vdsabev/google-docs-viewer-example
Failing function URL: https://google-docs-viewer.netlify.app/.netlify/functions/document/2PACX-1vT6_yEVCSJJHoiZAFOtoQYEh3A-qmLPW-ESjoyjYRgizhOVdOgsbhzzV8FhKNc3xJVidawPcuuFH87W

Latest publish log (cleared cache, no build step used):

1:03:54 AM: Build ready to start
1:03:55 AM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
1:03:55 AM: build-image tag: v3.4.1
1:03:55 AM: buildbot version: c6376102eedf4be6c6e5d685c7141e2eb612d47d
1:03:55 AM: Building without cache
1:03:55 AM: Starting to prepare the repo for build
1:03:56 AM: No cached dependencies found. Cloning fresh repo
1:03:56 AM: git clone https://github.com/vdsabev/google-docs-viewer-example
1:03:56 AM: Preparing Git Reference refs/heads/master
1:03:58 AM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'public' versus '/' in the Netlify UI
1:03:58 AM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'api' versus '' in the Netlify UI
1:03:58 AM: No build command found, continuing to publishing
1:03:58 AM: Starting to deploy site from 'public'
1:03:58 AM: Creating deploy tree 
1:03:58 AM: Creating deploy upload records
1:03:58 AM: 0 new files to upload
1:03:58 AM: 0 new functions to upload
1:03:58 AM: Starting post processing
1:03:58 AM: Post processing - HTML
1:03:58 AM: Finished processing build request in 3.209823701s
1:03:58 AM: Post processing - header rules
1:03:58 AM: Post processing - redirect rules
1:03:59 AM: Post processing done
1:03:59 AM: Site is live

Solutions attempted (and failed):

  1. Moved /package.json to /api/package.json
  2. Moved /package.json to /api/document/package.json and /api/document.js to /api/document/document.js
  3. Added npm install as prebuild script in package.json
  4. Added npm install as build script in package.json

I can’t think of anything special I’m doing with this project, or why it won’t work. Any suggestions?

Hey @vdsabev,
Sorry for the slow response here! It looks like your site doesn’t have a build command, so your functions aren’t getting built. You’ll need to set the command to # in your netlify.toml. Our docs mention this here:

Let us know if that doesn’t help and we’ll keep digging!

1 Like

Thank you @jen - you’re absolutely right, my other projects have npm run build defined as a build step in the web UI but not the netlify.toml config file, and so I missed that part. I’m confident I can now resolve my issue! If your dev team is open to some feedback, I have a few suggestions.

First, I did consider the fact that this project doesn’t have a build step as a factor, but assumed Netlify always deploys the functions since they’re not processed by the build tools I usually have (frontend, e.g. webpack / CRA). I’m sure there are some valid reasons, but to a developer like me who doesn’t specialize in devops it seems arbitrary that to publish some backend code you need to specify a command, even if it doesn’t do anything. Suggestion #1 - what if the Netlify build process defaulted to command = # for all projects, or at least for projects that have functions defined?

#2 - alternatively, what if Netlify automatically ran the build script if it’s defined in package.json?

Next, this excerpt from my app’s log shows Netlify clearly knows there’s a functions path declared, and that there’s NO build command:

1:03:58 AM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'api' versus '' in the Netlify UI
1:03:58 AM: No build command found, continuing to publishing

Since you said a build command is required for functions to work - #3 - what if instead of “No build command found, continuing to publishing” the message said something like “No build command found - functions will NOT be deployed, continuing to publishing”? Maybe even include that useful documentation link you so graciously pointed to?

Finally, #4 - instead of saying that a build command is required in the full text of the documentation, what if it showed that in the code example itself? Code examples are highlighted differently from the rest of the documentation and are the first thing I look at. Something like this would be nice:

[build]
  functions = "my_functions"
  command = "#" # Build command is required with functions, add your own or use '#' if no build is necessary

Thanks again for your help!

1 Like

These are awesome questions and suggestions! Thanks so much for taking the time to write them up.

To your questions about automatically building: we don’t want to do unnecessary build steps in cases where there’s nothing to build because we have to charge for that time. The # and entering the build command in the UI or command part of the netlify.toml is to let people opt in and give control over what we do with their code.

I definitely do think it’d be great to update that log line about what happens when you have a functions directory but no build command. I will get an issue filed and we can update you here if/when that ships. And can also bring your suggestion about a code sample with the # build command to our docs team. Many thanks again :pray:

1 Like

Relevant to your interests @vdsabev :slight_smile: --> Change coming November 9: No build command required for continuous deployment of Netlify Functions Thanks again for your input

1 Like