Thanks to visit codestin.com
Credit goes to github.com

Skip to content

File operations in functions using fs fail #914

@mbifulco

Description

@mbifulco

(note, this is carried over from a discussion in #706)

edit: related code is in this PR

Hey there - I'm trying to implement a sitemap.xml for my site, and running into problems when my site gets deployed to netlify. It seems to have to do with my use of path.join() in my code to iterate over the files that comprise content on my site.

My current strategy is to look through the /pages directory in my code to create an XML entry for each, using syntax like this:

path.join(process.cwd(), 'src', 'pages');

This runs fine locally in dev, and when deployed on Vercel, but when I deploy to netlify, I'm seeing an error in functions/___netlify-handler when I try to visit the url generating my sitemap:

11:21:47 AM: 92378b15 ERROR  Error: ENOENT: no such file or directory, scandir '/var/task/src/pages'
    at Object.readdirSync (fs.js:1043:3)
    at getStaticPageUrls (/var/task/.next/server/pages/api/sitemap.xml.js:56:47)
    at handler (/var/task/.next/server/pages/api/sitemap.xml.js:212:28)
    at Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils.js:102:15)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Server.handleApiRequest (/var/task/node_modules/next/dist/server/next-server.js:1014:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/next-server.js:901:37)
    at async applyCheckTrue (/var/task/node_modules/next/dist/server/router.js:93:32)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:231:25)
    at async Server.run (/var/task/node_modules/next/dist/server/next-server.js:1085:29) {
  errno: -2,
  syscall: 'scandir',
  code: 'ENOENT',
  path: '/var/task/src/pages'
}

In my case, I'm using an API Route at /pages/api/sitemap.xml.js to generate the sitemap, and pairing that with a redirect in next.config.js to send the output of that function to /sitemap.xml:

module.exports = {
    // ...
    rewrites: async () => [
      {
        source: '/sitemap.xml',
        destination: '/api/sitemap.xml',
      },
    ],
};

I'm wondering if there's something tricky going on here because of how functions are deployed. Any guesses if this might be related to this plugin? Note that this appears to be broken even if I visit the non-redirected page /api/sitemap.xml

Originally posted by @mbifulco in #706 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions