HubSpot UI Extension | Serverless Function Fails to Find Local Module After Deployment in HubSpot UI
Hi There,
I'm deploying a custom UI Extension in HubSpot that includes serverless functions. Locally, everything works fine, but once I deploy, I encounter the following error:
{
"status": "error",
"message": "The serverless function 'handle-report' failed to execute: Cannot find module './services/handle-report-service'\nRequire stack:\n- /var/task/test.js\n- /var/task/hubspotHandler.js\n- /var/runtime/index.mjs."
}
I'm attempting to require a local module from in 'handle-report' which is a serverless function:
In local development, this works perfectly, and the module is found as expected. However, once the extension is deployed in the HubSpot environment, using 'hs project upload' the serverless function fails with a "Cannot find module" error.
What I've Tried:
Using Relative Paths (./services/handle-report-service😞
Using path.join() and path.resolve() with __dirname:
Testing process.cwd():
Double-checking the folder structure:
The services/handle-report-service.js file is present locally and is deployed as part of the extension.
Checking deployment settings and logs:
I reviewed the deployment logs, but nothing indicates why this file isn't being found.
The issue likely stems from how HubSpot handles paths in the serverless environment after deployment. In a serverless setup, the directory structure may differ from local development, so relative paths don't always behave as expected.
Here’s what you can do:
1. Check that the filename and the import path match exactly, including case sensitivity. Serverless environments may be stricter about this than local ones.
2. Try switching from relative paths (./services/handle-report-service) to absolute paths by using path.resolve() to ensure the correct module path:
3. Verify that the services/handle-report-service.js file is included in the build when you deploy the project using hs project upload. Sometimes, files may not be uploaded if excluded in deployment settings.
4. If the issue persists, try cleaning the project (delete build artifacts) and re-deploying it using hs project upload.
This should help resolve the path issue in the serverless environment.