Changes in internal paths of files deployed to Netlify Functions

Hi, everyone. Happy Friday! :partying_face:

I want to share with you a change to some internal implementation details of Netlify Functions that we’ve recently rolled out.

Up until now, all function files were placed inside a src/ sub-directory when deployed. This meant that there was a mismatch in file paths between the local and production environments.

For example, imagine the following project structure:

my-project/
β”œβ”€ netlify/
β”‚  β”œβ”€ functions/
β”‚  β”‚  β”œβ”€ my-function.js
β”‚  β”‚  β”œβ”€ file-one.txt
β”œβ”€ netlify.toml

When developing locally, file-one.txt lives at {PROJECT ROOT}/netlify/functions/file-one.txt, but when deployed to production its path would be {FUNCTION ROOT}/src/netlify/functions/file-one.txt (note the extra src/ path segment).

This meant that if you wanted to dynamically reference this file, and you were constructing its path using something like process.cwd() in Node, you would get inconsistent results between the local and production environments.

The fix we rolled out addresses this inconsistency and removes the extra src/ segment from the path, meaning that the file will live at {FUNCTION ROOT}/netlify/functions/file-one.txt in production.

What this means for you

Nothing has changed in our public APIs or in the way files are referenced. For example, any files you’re referencing using relative paths (e.g. require('../../some-file.js')) will continue to work in the same way.

This change should go unnoticed for the vast majority of users.

Only if you are using absolute paths starting with /var/task you might need to update them. For example, a path like /var/task/src/some-file.js must be changed to /var/task/some-file.js.

I should add that if you are using these paths, you should find an alternative solution, because you’re relying on internal implementation details that are subject to change. If that’s your case and you need help in finding alternative solutions, share your use case with us and we’ll be glad to point you in the right direction.

Let me know if you have any questions about this. Thanks! :raised_hands:

1 Like