Hono Node runtime won't deploy on vercel

I am trying to deploy the hono example project with nested routes but I keep getting the error below

ReferenceError: exports is not defined in ES module scope

it runs fine locally

Looks like this issue reported here from march hasn’t been resolved

I’ve tested a 2x2x2x2 matrix of factors — JS vs TS, Edge vs Node runtime, dev vs prod, and package.json type: module vs not — and here are the results:

Lang Runtime type:module Dev Prod
JS Edge type:module :white_check_mark: :white_check_mark:
JS Edge :white_check_mark: :white_check_mark:
JS Node type:module :white_check_mark: :white_check_mark:
JS Node :white_check_mark: :white_check_mark:
TS Edge type:module :white_check_mark: :white_check_mark:
TS Edge :white_check_mark: :white_check_mark:
TS Node type:module :white_check_mark: :x:
TS Node :x: :white_check_mark:

Error details:

  • TS/Node/type:module/prod: ReferenceError: exports is not defined in ES module scope
  • TS/Node/—/dev: Error: Unexpected token 'export'

Hi, Arpit! Welcome to the Vercel Community :smile:

As far as I understand, this error typically occurs when there’s a mismatch between CommonJS and ES Module syntax. I have a few suggestions!

  1. First, make sure your package.json file includes the following:
{
  "type": "module"
}

This tells Node.js to treat all .js files in your project as ES modules.

  1. Update your import statements to use ES Module syntax. For Hono, it should look like this:
import { Hono } from 'hono'
  1. If you’re using TypeScript, ensure your tsconfig.json includes:
{
  "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "node"
  }
}
  1. If you’re using any CommonJS modules in your project, you might need to update them to use ES Module syntax or use dynamic imports.
  2. Make sure you’re using a recent version of Node.js that fully supports ES Modules (Node.js 14.x or later is recommended).
  3. If you’re using a bundler like webpack or rollup, ensure it’s configured to handle ES Modules correctly.

If you’re still encountering issues after making these changes, it would be helpful to see more of your code, particularly the file where the error is occurring and any configuration files you’re using.

1 Like

Hi Paulina, I’ve already done these things. I don’t think you read through my explanation, I’ve linked through the orignal post where he does a much better job of explaining the issue. . There is even a table which points on which instance this doesn’t work.

This is bug on your deployment system.

I saw your error and jumped to solution-ising! Apologies :pray:

It would be helpful to get a reproducible example from you as well, Arpit. I will do the same :slight_smile: then pass it on to the team!

1 Like

I found the repro buried in the original thread and passed it along to the team. Thanks for reporting this again.

It only works for me if I build from my local copy and deploy the prebuilt project. So that’s a possible workaround until we can identify the cause and reach a more stable solution.

We’ll keep you updated here as we learn more from the deployment experts

The team just got back to me with the solution. The example repo you shared already had "type": "module" in the the package.json, but no tsconfig.

I copied Pauline’s recommended config from above and my deployment started working as expected. Please make sure you have the project setup so TS files output ES instead of CommonJS. Then the error will go away :slightly_smiling_face:

1 Like

I just ran into this issue myself, the culprit is that the Hono starter has the tsconfig in the .vercelignore file so even with the correct settings, it won’t work when you deploy

That’s also why your workaround by building locally worked, since the tsconfig was present in your local system even if it wasn’t being deployed

I’ve made a PR to fix the vercelignore file

3 Likes

Ah that makes sense, I removed it from the .vercelignore it working for me now :+1: thank you

3 Likes

@jacobparis Good catch! Thanks for the PR :smile:

2 Likes

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