I can't seem to import local modules

I am trying to import local modules but I get an error

Current versus Expected behavior

Here’s the error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/api/openai' imported from /var/task/api/routes/aiRoutes.js
    at finalizeResolution (node:internal/modules/esm/resolve:269:11)
    at moduleResolve (node:internal/modules/esm/resolve:937:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)
    at defaultResolve (node:internal/modules/esm/resolve:1216:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:540:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:509:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
    at link (node:internal/modules/esm/module_job:95:36) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///var/task/api/openai'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
INIT_REPORT Init Duration: 240.76 ms	Phase: invoke	Status: error	Error Type: Runtime.ExitError

I would expect it to act normal and import the dang module

Code, configuration, and steps that reproduce this issue

Here’s the code it doesn’t like:

import { setupAIRoutes } from './routes/aiRoutes.js';
import { setupLicenseRoutes } from './routes/licenseRoutes.js';

Here’s the vercel.json file (I tried adding the imports to the bundle it didn’t help):

{
  "rewrites": [
    {
      "source": "/api/(.*)",
      "destination": "/api"
    }
  ],
  "redirects": [
    { "source": "/", "destination": "/api", "permanent": false }
  ],
  "functions": {
    "api/index.js": {
      "includeFiles": "api/**"
    }
  }
}

Here’s package.json

{
  "type": "module",
  "engines": {
    "node": "20.x"
  },
  "scripts": {
    "start": "vercel dev",
    "deploy": "vercel"
  },
  "dependencies": {
    "@ai-sdk/openai": "^0.0.58",
    "@hono/node-server": "^1.12.2",
    "ai": "^3.3.34",
    "hono": "^4.4.2",
    "openai": "^4.59.0",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "vercel": "^32.4.1"
  }
}

Project information

Deployment URL: https://vercel.com/geoidesics-projects/actor-studio-openai/DWAzrWceD272QMM6Jjra3reFsiPZ
Environment (local, preview, production): production
Project Framework: Hono
Build Settings:
  Framework Preset: Hono (https://vercel.com/templates/hono/hono-on-vercel)
  Build Command (if not default): vercel --prod
  Output Directory (if not default):
  Install Command (if not default): 
Node/Runtime Version: 20.x
Package Manager: yarn
Relevant Packages: see package.json above

Hey @geoidesic. I have a working example app based on the Hono on Vercel starter, but with local files imported. It’s an overly simple example, but it might help as a reference point. :slightly_smiling_face:

Deployment URL: hono-1270-kk9wy4suq-amy-vtest314.vercel.app
Project Framework: Hono
Build Settings:
  Framework Preset: Other
  Build Command (if not default): 
  Output Directory (if not default):
  Install Command (if not default): 
Node/Runtime Version: 20.x

Thanks. It looks identical to mine. Still mystified why it won’t work for me. The only difference is that I’m trying to include from a subfolder whereas you’re including from the root folder or current module.

I tried moving one of the local imports to a subdirectory within API, and worked in my example app.

Deployment: hono-1270-oj5wy0ioe-amy-vtest314.vercel.app
Commit: Move to sub-directory · amyegan/hono-1270@2fc5d10 · GitHub

Another difference I noticed is my project does not include functions configuration in the vercel.json file. Does it make a difference for your project if you change that?

No, it does not. I added that in an attempt to fix the error. Tomorrow I will try use your project as a starting point and see if I can morph it back into my project while retaining the import ability. Thank you for your efforts to help me with my query!

1 Like

It was down to other imports not including the .js extension in the import path.

1 Like

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