Build failing when using node-fetch in a serverless function

I’m having issues with a build failing. I have attempted to use a serverless function, but from what I can tell the build fails because of the function dependencies. I’m using node-fetch as a dependency. The build seems to fail due to a version of Ruby maybe?

Hoping to get some assistance with this. I’m new to serverless functions, so could easily be something I am overlooking!

This is where things start to go wrong:

ERROR: Missing proper 'which' command. Make sure it is installed before using RVM!
10:59:59 PM: WARNING:
10:59:59 PM:       Errors sourcing '/opt/buildhome/.rvm/scripts/base'.
10:59:59 PM:       RVM will likely not work as expected.
10:59:59 PM: Attempting ruby version 2.6.2, read from environment
11:00:00 PM: ERROR: Missing proper 'which' command. Make sure it is installed before using RVM!
11:00:00 PM: RVM not loaded, aborting.
11:00:00 PM: 
11:00:00 PM: ** WARNING **
11:00:00 PM: Using custom ruby version 2.6.2, this will slow down the build.
11:00:00 PM: To ensure fast builds, set the RUBY_VERSION environment variable, or .ruby-version file, to an included ruby version.
11:00:00 PM: Included versions: WARNING: Errors sourcing '/opt/buildhome/.rvm/scripts/base'. RVM will likely not work as expected.
11:00:00 PM: 
11:00:00 PM: ERROR: Missing proper 'which' command. Make sure it is installed before using RVM!
11:00:00 PM: WARNING:
11:00:00 PM:       Errors sourcing '/opt/buildhome/.rvm/scripts/base'.
11:00:00 PM:       RVM will likely not work as expected.
11:00:00 PM: RVM not loaded, aborting.
11:00:00 PM: Failed to install ruby version '2.6.2'
11:00:00 PM: npm
11:00:00 PM:  ERR!
11:00:00 PM: code ELIFECYCLE
11:00:00 PM: npm
11:00:00 PM: ERR! errno 1
11:00:00 PM: npm ERR!
11:00:00 PM:  gatsby-theme-personal-site@1.0.0 build:functions: `build functions`
11:00:00 PM: npm ERR! Exit status 1
11:00:00 PM: npm
11:00:00 PM:  ERR!
11:00:00 PM: npm
11:00:00 PM:  ERR!
11:00:00 PM:  Failed at the gatsby-theme-personal-site@1.0.0 build:functions script.
11:00:00 PM: npm ERR!
11:00:00 PM:  This is probably not a problem with npm. There is likely additional logging output above.
11:00:00 PM: npm ERR! A complete log of this run can be found in:
11:00:00 PM: npm ERR!
11:00:00 PM:      /opt/buildhome/.npm/_logs/2020-04-11T22_00_00_117Z-debug.log
11:00:00 PM: npm
11:00:00 PM:  ERR! code ELIFECYCLE
11:00:00 PM: npm
11:00:00 PM: ERR! errno
11:00:00 PM:  1
11:00:00 PM: npm
11:00:00 PM:  ERR! gatsby-theme-personal-site@1.0.0 prod: `npm run build; npm run build:functions`
11:00:00 PM: npm
11:00:00 PM: ERR! Exit status 1
11:00:00 PM: npm ERR!
11:00:00 PM: npm
11:00:00 PM:  ER

Hiya @nickwarters - mind linking us to the build logs in our UI? There are a lot of per-site details stored in our database and it’s pretty impossible to advise on weird error messages like this that aren’t common, without checking those settings.

Hi!
Sure thing, here’s a link to a failed build. Netlify App

Hey @nickwarters,
I took a look at your logs and I’m not sure what the nvm is not compatible with the "npm_config_prefix" environment variable error is. I do see that you have a build:functions command somewhere:
gatsby-theme-personal-site@1.0.0 prod: npm run build; npm run build:functions
Do you want to try to deleting that and seeing if that fixes things for you? When you have a “functions” directory declared in your netlify.toml file, Netlify will find it and bundle your function for you automatically. More on setting up your netlify.toml for that here:

Also wanted to share some resources since you mentioned that you’re new to serverless functions :slight_smile:

and here:

Hi @jen,

Thank you for this it seems to have built the site okay. Now I have another problem. The function I have built uses node-fetch. I have installed node fetch, and when I call the function I’m getting a 502 error saying node-fetch could not be found in the stack:
{“errorType”:“Runtime.ImportModuleError”,“errorMessage”:“Error: Cannot find module ‘node-fetch’\nRequire stack:\n- /var/task/form-submission.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js”,“stack”:[“Runtime.ImportModuleError: Error: Cannot find module ‘node-fetch’”,“Require stack:”,"- /var/task/form-submission.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object. (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1158:30)"," at Object.Module._extensions…js (internal/modules/cjs/loader.js:1178:10)"," at Module.load (internal/modules/cjs/loader.js:1002:32)"," at Function.Module._load (internal/modules/cjs/loader.js:901:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)"," at internal/main/run_main_module.js:18:47"]}

Great, making progress! As for node-fetch, I think this post will be helpful:

And I believe node-fetch has a peer dependency, the encoding module, so you may have to install that if you don’t have it already. It’s mentioned in that thread.

Let us know how it goes!

Hey @jen !
So I have added the following:

"scripts": {
"build": "cd functions/form-submission && npm install && gatsby build",

No luck

I have also added variations on this using “prebuild” which I read somewhere else may help, no luck.
Finally I have also added the encoding dependency as you mentioned but no luck.

I’m still getting the same 502 error of:

6:02:54 PM: 2020-04-17T17:02:54.181Z	undefined	ERROR	Uncaught Exception 	 
{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'node- 
fetch'\nRequire stack:\n- /var/task/form-submission.js\n- /var/runtime/UserFunction.js\n- 
/var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'node- 
fetch'","Require stack:","- /var/task/form-submission.js","- /var/runtime/UserFunction.js","-     /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1158:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)","    at Module.load (internal/modules/cjs/loader.js:1002:32)","    at Function.Module._load (internal/modules/cjs/loader.js:901:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}
6:02:54 PM: 2020-04-17T17:02:54.805Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/form-submission.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'","Require stack:","- /var/task/form-submission.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:100:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1158:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)","    at Module.load (internal/modules/cjs/loader.js:1002:32)","    at Function.Module._load (internal/modules/cjs/loader.js:901:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)","    at internal/main/run_main_module.js:18:47"]}
6:02:54 PM: Duration: 187.30 ms	Memory Usage: 14 MB	
6:02:54 PM: Unknown application error occurred
Runtime.ImportModuleError

I think that since you cd into functions, you’ll have to cd back out in order to build. Something like:
"build": "cd functions/form-submission && npm install && cd ../ && gatsby build",

Want to give that a shot?

Hey @jen,
Okay so I seem to have got it working!

Thank you so much for your help!

1 Like