Does serverless function supports require-ESM on Node 22? In this blog post, it mentions “added support for require()ing synchronous ESM module graphs”. However, it doesn’t work in my deployment.
Current behavior
When I require an ESM-only library, e.g. uuid, Node throws ERR_REQUIRE_ESM.
Expected behavior
Node doesn’t throw when requiring an ESM-only library.
Hi @alvin-leung-concept4, I’m unable to recreate the local behaviour without using the --experimental-require-module flag.
This functionality was initially introduced in Node.js versions 22.0.0 and 20.17.0 behind the --experimental-require-module flag. In later versions, specifically Node.js v23.0.0, the feature was no longer behind this flag, and in v23.5.0, the experimental warning associated with it was removed by default.
I’m digging more to find a solution for you. Will share here when I have a working example.
Hi @alvin-leung-concept4, I was able to run your code on Vercel Nodejs v22 by providing the flag in the NODE_OPTIONS environment variable using vercel.json:
I couldn’t replicate your success. I have used your vercel.json, and it still doesn’t work.
I see the flag --no-experimental-require-module in process.execArgv and it overrides the --experimental-require-module flag in NODE_OPTIONS. Not sure how your deployment has the opposite flag set.
I have try deploying using both GitHub integration and Vercel CLI, and the deployments always have the flag --no-experimental-require-module set in process.execArgv.