Cannot find module

I’m trying to create a simple Vercel function running on a node environment using the Hono framework.

Using these steps: Vercel - Hono

npm create hono@latest my-app

npm i

instead of “npm run dev” I must call “npm run start,” but this is Hono problem.

npm i @hono/node-server

replacing handle from “import { handle } from ‘hono/vercel’” to “import { handle } from “@hono/node-server/vercel”;”

Creating .env file and adding: NODEJS_HELPERS=0

Also adding to package.json “type”: “module”

starting the server: npm run start

opening the browser: http://localhost:3000/api and seeing “{“message”:“Hello Hono!”}”

Everything looks great.

Now I’m adding the test.ts file into /api

Contents: export const myName = "Bob";

In /api/index.ts changing

return c.json({ message: "Hello Hono!" });

to

return c.json({ message: `Hello ${myName}` });

and adding
import: import { myName } from "./test";

starting the server: npm run start

no errors

Opening in browser: http://localhost:3000/api

and getting the error: Error: Cannot find module ‘…\test\vercel\my-app\api\test’ imported from …\test\vercel\my-app\api\index.ts

Hey @services-faradaylabs. The problem you described seems to be related to local development instead of Vercel deployment.

I’m not very familiar with Hono. I think the team who develops the framework would know more about what’s going wrong in this case. I wouldn’t want to give you bad advice about it.

I don’t think it’s a hono problem. I found the exact problem with the Vercel a year ago. Check out this link:'ERR_MODULE_NOT_FOUND' on Vercel Serverless API typescript · vercel · Discussion #1226 · GitHub This is exactly my situation, except I have vercel cli v35 not v28 as described in the link.

The error refers to a missing module import and you mentioned that the error is seen on http://localhost:3000/api

Is this happening only when you deploy the app to Vercel, or is it happening when you run it locally?

module is not missing: its right there. I cannot import any other file inside index.ts It’s not possible to write code in a single file. I’ve made a simple example - created new file near index.ts and it’s not found. Don’t have a clue what to do at all.

Can you share the example app so I can debug with you?

Sure. Here is the download link to dropbox, I zipped a whole folder:
https://www.dropbox.com/scl/fi/nbyhc8hut839puwsu0xo9/my-app.zip?rlkey=baz4hpt6p53qwpvbsr95yq71w&dl=0

On the top right there will be an arrow button for downloading. After clicking - popup will appear, to sign in, but at the bottom of the popup there will be a download link.

Dropbox makes it harder to download zip files nowadays…

Link to the app: https://my-app-rho-three-34.vercel.app/api

The zip file is a bit too large for me to download since node_modules were included, but I was able to take a look at the project structure at least.

I attempted to replicate it using Hono’s Next.js starter template (i.e. npm create hono@latest my-app and selected the nextjs option).

I added a /pages/api/test.ts file with export const myName = "Bob"; as its only contents. I added import { myName } from './test' to my /pages/api/[...route].ts file and ran vercel dev. I also updated the message to output the value of myName to be sure the import actually worked as expected.

When I visited http://localhost:3000/api/hello I saw the expected message. However, it also created an unnecessary extra serverless function. This is because all .js and .ts files in the /pages/api directory are automatically assumed to be serverless functions.

Next, I created a /utils folder and moved the test.ts file into it. I updated /pages/api/[...route].ts with import { myName } from '../../utils/test' to match the new file location. I visited http://localhost:3000/api/hello again running the updated code and got the same expected message.

I pushed my example code to a public repo so you can see what I did: GitHub - amyegan/hono-nextjs

Thank you for your help :wink:

Think is, I don’t know next.js - never worked with it. That’s why during project creation I didn’t select next.js template. I selected: vercel template right away.

After projet creation, I made changes which are shown for node.js. Scroll the page down , after point 4 Node.js:

I reuploaded the project without node modules, it’s only 112KB now:
https://www.dropbox.com/scl/fi/nbyhc8hut839puwsu0xo9/my-app.zip?rlkey=baz4hpt6p53qwpvbsr95yq71w&dl=0

Also added public github repo:

But it’s missing .env file - NODEJS_HELPERS=0

Any updates on this topic are highly appreciated!

I created a small example of a serverless function that imports another file from the same repo. It works if I include the .js file extension in my simple example. But some additional configuration is likely needed with Hono and TypeScript.

The Hono folks would know more about how the framework actually works and their recommended setup for what you’re trying to do with that starter project.

My minimal example repo:

1 Like

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