Frameworkless serverless function import cannot find module

I have a relatively simple setup that essentially bundles some source code and outputs to dist using esbuild. This works and I can see it in the output tab of my function.

However, when I attempt to import it from my serverless function in the api folder, i.e. “api/x.ts”, it cannot find the module.

The code itself is very simple for x.ts

export { GET } from "../handler.js"

with the output directory structure being:
api/
├─ x.js
├─ middleware
handler.js
middleware

This is running on node 22.x. I cannot determine why I cannot import handler.js from my serverless function. I’ve added “includeFiles”: “handler.js” to my vercel.json.

The actual error is:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module ‘/var/task/handler.js’ imported from /var/task/api/x.js

Thank you

Resolved this after many different configurations of paths! A dynamic import was required and moreover the path was not “handler.js” but rather “dist/handler.js”.

I think this is a consequence of using vercel cli and having outputDir defined but not rootDir. Though the Vercel functions UI showing the output structure mentioned above is misleading, at least in this case.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.