Support Forums

Next.js: Node version for functions (server-side code) can't be changed

Hi Everyone!

I played around with deploying a Next.js site to Netlify but unfortunately I fail to run the server-side code (getStaticProps etc.). Since I use fs/promises in the sever-side code, I need NodeJS version 14+ but Netlify uses version 12.

I did set the AWS_LAMBDA_JS_RUNTIME environment variable to nodejs14.x as instructed on this page: Build serverless functions with JavaScript | Netlify Docs

I also did set that variable in the web UI, so this thread shouldn’t apply: AWS_LAMBDA_JS_RUNTIME - nodejs14.x

I guess I could use require('fs').promises instead of require('fs/promises'), but the problem is a general one: Changing the NodeJS version for Netlify functions fails (for me) when deploying a NextJS app with Netlify NextJS Plugin.

The site’s name is sharp-heisenberg-a36793 and the key error output can be found below:

Error message
5:52:38 PM:   A Netlify Function failed to require one of its dependencies.
5:52:38 PM:   Please make sure it is present in the site's top-level "package.json".
5:52:38 PM:   In file "/opt/build/repo/netlify/functions/next_courses/next_courses.js"
5:52:38 PM:   Cannot find module 'fs/package.json'
5:52:38 PM:   Require stack:
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/resolve.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/node_dependencies/index.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/node_modules/@netlify/zip-it-and-ship-it/src/main.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/plugins_core/functions/index.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/commands/get.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/main.js
5:52:38 PM:   - /opt/buildhome/.netlify-build-nvm/versions/node/v12.16.3/lib/node_modules/@netlify/build/src/core/bin.js

To me, that looks like Netlify is using Node v12 for the server-side functions.


Welcome to the forum @Maximilian_Schwarzmu

Do you have fs in your package.json?

There is a NODE_VERSION environment variable to specify a different node version than is used in the build system. Have you tried that?

You can also change the build image used for your site.

1 Like

fs is a NodeJS core module - it doesn’t have to be added to package.json (see File system | Node.js v16.8.0 Documentation and fs - npm).

I did set NODE_VERSION to 16, so that also has no impact.

Regarding the build image, Ubuntu Focal 20.04 is selected - i.e. the image with all the latest versions.

So that should all be good, still the above mentioned error occurs.

What if you set NODE_VERSION to v14.17.1?

This didn’t change anything, the error message is still the same unfortunately.

Maybe it’s the NextJS Netlify Plugin? Is it possible that this plugin overwrites my AWS LAMBDA Node version and always sets it to 12?

I would doubt it.

Have you tried this? Or perhaps tried

const fs = require('fs');


If you are requiring a data file have you tried something like

const myFile = require('../file.json');

Or perhaps you could enable esbuild

Yeah, I did work around it now by importing it differently. But this doesn’t solve the original problem: Netlify ignores my AWS Lambda NodeJS version choice.

Hey there, @Maximilian_Schwarzmu :wave:

Thanks for you patience here. One of our Support Engineers just took a look, and it appears as though you now have a successful deploy. Can you confirm if you are still experiencing issues or not? Let us know!

Hi @hillary

It’s deployed now because I used the work around mentioned.

If I use the import from ‘fs/promises’, the issue still persists (i.e. I can’t upgrade to a higher Node version for executing the lambda functions).


Hi @Maximilian_Schwarzmu,

I believe there’s something wrong going on here. Yes, it appears that the Environment Variable is not working, or at least the package is not loading correctly. I’d get more information on this from the devs.

On a side note, this appears to work fine when using esbuild. Could you try that and let us know?

I don’t know, I’m using the regular NextJS build process (npm run build) which uses Webpack under the hood. I haven’t tried setting up a custom build workflow that would use another build tool like esbuild.

I can image that it might work with another tool which, under the hood, transforms fs/promises to the working workaround mentioned earlier in this thread.

But it’s easier to implement the workaround in the code myself - I’m not a build process expert.

Either way, I would expect it to “just work” on Netlify since we (normally) have the option of using a more recent NodeJS version for the cloud functions.

npm run build does use Webpack as that’s used by Next.js. However, for functions, we’ve 2 options: zisi and the more recent one is esbuild. It’s not just adding a transformation, it’s basically using a different bundler which might have better way of handling model resolutions.

Either way, you don’t have to make a switch especially if the workaround already works for you. While a lot of config on Netlify usually works automatically, somethings might need some user input.