Function gives 502 error when deployed to Netlify but executes fine locally

Hi,

I am in the process of deploying my new website to Netlify but having some issues with getting a function to correctly execute when deployed.

Currently I get a 502 error when the function is deployed to Netlify, locally it executes fine with no errors and returns all the correct data. When I visit the URL of the function in my browser I am greeted with the below error:

{“errorType”:“Runtime.UserCodeSyntaxError”,“errorMessage”:“SyntaxError: Unexpected token ‘.’”,“trace”:[“Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token ‘.’”," at _loadUserApp (/var/runtime/UserFunction.js:98: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:1015:30)"," at Object.Module._extensions…js (internal/modules/cjs/loader.js:1035:10)"," at Module.load (internal/modules/cjs/loader.js:879:32)"," at Function.Module._load (internal/modules/cjs/loader.js:724:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}

The function I’m having issues with is my ‘twitter’ function which is deployed @ my website

I also have a second function (‘emailSignup’) which executes fine.

You can view the code for both functions @ my GitHub

I have tried various solutions from the forums but haven’t had any luck so far. Below is a couple of the links I’ve tried:

I did have some issues deploying the site due to the node version on the Netlify deploy being different to my local version of node. I have since set the env vars on Netlify to use the same version of node I’m using locally (v14.15.1) and now the site deploys fine but just this one function doesn’t work.

Any help is greatly appreciated.

Thank you,

Hi! I don’t know if this helps you but, I’ll give it a try. :slightly_smiling_face:

I made a issue on Nuxt and it helped me. The problem for me was that i had node v12 on my local development, but Netlify installed 8.17. I had to change the node version manually in Environment variables setting on Netlify. Check under build and deploy.

This is the issue, https://github.com/nuxt/nuxt.js/issues/8031

Edit: haha! I didn’t read your text at the end… I guess my solution don’t work then. :grinning_face_with_smiling_eyes:

Hi. Yeah the node version admittedly took me a few hours to figure out as it stopped my main site deploying.

But, as soon as that issue resolved I got launched into this one and can’t figure out.

I can’t find a reason why the function shouldn’t work, it works locally with no issues at all but as soon as it’s deployed to Netlify just errors out.

Any other ideas are welcome.

I have resolved the issue, below is what I believe was causing the issue and how I resolved it.

After spending considerable time researching different parts of Netlify Functions and my code. I came to the conclusion that the site was setup correctly for Functions to work as I had another function running on the site without errors.

This meant that the issue was with that individual function. As I knew the function worked locally I was inclined to think that there was an issue with the code so I removed all processing of the data and simply returned the data I was fetching, this worked!

So I managed to narrow down the issue to the below code:

const tweets = data.map((tweet) => {
const source = tweet.referenced_tweets === undefined ? ‘original’ : tweet.referenced_tweets[0].type;
const mediaKey = tweet.attachments?.media_keys[0];
const [media] = includes.media.filter((obj) => obj.media_key === mediaKey);
return { …tweet, …media, source };
})

Then I did some researching and found out that Netlify Functions based on Amazon Lambda currently only supports Node 12 [source]

So looking at the above code I found the issue in the conditional chaining operator (?.) that is only supported in Node 14 which is what I was using locally hence why it worked locally.

So I changed that code to the below:

const tweets = data.map((tweet) => {
const source = tweet.referenced_tweets === undefined ? ‘original’ : tweet.referenced_tweets[0].type;

const mediaKey = tweet.attachments && tweet.attachments.media_keys[0];
const media = includes.media.filter((obj) => obj.media_key === mediaKey)[0];
return { ...tweet, ...media, source };

});

And now it works!

Note to self check compatibility of code before using the latest and greatest parts of a language.

TLDR: Used a conditional chaining operator in my code that requires Node 14 but the code was running on Node 12 causing the issue. Changed the conditional chaining operator out and function works as expected.

thanks so much for sharing your process & solution!