Support Forums

How to load file in publish directory

I was trying to create a simple search function for use on a Jekyll site.

Is there a way to load templates and data files generated by Jekyll in Netlify Functions?

It worked on local, but not when I actually deploy it.


  functions = "_functions"
  from = "/search"
  to = "/.netlify/functions/search"
  status = 200
  force = true
  from = "/search.html"
  to = "/search"
  force = true
  from = "/search-index.json"
  to = "/404.html"
  status = 404
  force = true



const fs = require('fs');
const path = require("path");

const TEMPLATE_PATH = path.join(__dirname, "../_site/search.html");
const INDEX_PATH = path.join(__dirname, "../_site/search-index.json");

exports.handler = async(event, context) => {
  let template = fs.readFileSync(TEMPLATE_PATH, 'utf8');

  let index = index = JSON.parse(fs.readFileSync(INDEX_PATH, 'utf8'));



Directory structure:

  • _functions
    • search.js
  • netlify.toml
  • search-index.js
  • search.md
  • directories and files for jekyll build

Build command

JEKYLL_ENV=production jekyll build

Error message:

{“errorType”:“Error”,“errorMessage”:“ENOENT: no such file or directory, open ‘/var/_site/search.html’”,“trace”:[“Error: ENOENT: no such file or directory, open ‘/var/_site/search.html’”," at Object.openSync (fs.js:462:3)"," at Object.readFileSync (fs.js:364:35)"," at Runtime.exports.handler (/var/task/search.js:10:17)"," at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}

Hi @salmonspace

In sort, it won’t work. Functions are not deployed alongside your site so there can have no awareness of, or access to, your deployed site.

If you want a site search, you might like to look at the Algolia Search Plugin which indexes your site at build time.