Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load local file using Netlify functions

I've written a script which takes a JSON file and outputs it to an API endpoint using Netlify's Functions feature (https://functions.netlify.com/). For the most part, this works without a hitch, however, one of my endpoints has a lot of text and for ease of editing, I've split the large text blocks into markdown files which I then loaded into the endpoint.

Locally, this works perfectly, but when deployed I get a console error saying Failed to load resource: the server responded with a status of 502 (). I presume this is because I used a node fs method and Netlify doesn't allow that, however, I can't find any information about this.

The code I've used is here:

const marked = require('marked')
const clone = require('lodash').cloneDeep
const fs = require('fs')
const resolve = require('path').resolve
const data = require('../data/json/segments.json')

// Clone the object
const mutatedData = clone(data)

// Mutate the cloned object
mutatedData.map(item => {
  if (item.content) {
    const file = fs.readFileSync(resolve(`./src/data/markdown/${item.content}`), 'utf-8')
    item.content = marked(file)
  }
})

exports.handler = function(event, context, callback) {
  callback(null, {
    statusCode: 200,
    body: JSON.stringify({data: mutatedData})
  });
}

I've also attempted to replace

  const file = fs.readFileSync(resolve(`./src/data/markdown/${item.content}`), 'utf-8')

with

  const file = require(`../data/markdown/${item.content}`)

but that complains about a loader and I'd like to avoid adding webpack configs if possible as I'm using create-react-app, besides, I doubt it will help as I'd still be accessing the file-system after build time.

Has anyone else come across this issue before?

like image 383
Alex Foxleigh Avatar asked Jul 08 '26 22:07

Alex Foxleigh


1 Answers

At the time when this answer is written (September 2019), Netlify does not seem to upload auxiliary files to AWS Lambda, it appears that only the script where the handler is exported will be uploaded. Even if you have multiple scripts exporting multiple handlers, Netlify seems to upload them into isolated "containers" (different AWS instances), which means the scripts will not be able to see each other in relative paths. Disclaimer: I only tested with a free account and there could be settings that I'm not aware of.

Workaround:

For auxiliary scripts, make them into NPM packages, add into package.json and require them in your main script. They will be installed and made available to use.

For static files, you can host them on Netlify just like before you have AWS Lambda, and make http requests to fetch the files in your main script.

like image 58
Fan Zeng Avatar answered Jul 11 '26 11:07

Fan Zeng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!