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 functions | Netlify Docs
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.
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.
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!
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.