Home > Mobile >  'ERR_MODULE_NOT_FOUND' on Vercel Serverless API typescript
'ERR_MODULE_NOT_FOUND' on Vercel Serverless API typescript

Time:01-12

I've created a Vite project (Vue3 and Typescript) and thank to Vercel I've also created different endpoints that allow retrieving some data.

My idea is to have a \api folder with all the endpoints, and another \backend with all the business logic, so the file system would be like this:

api\
    index.ts
backend\
    enum.ts
    consts.ts
    logic.ts
src\
    ... vue files ...

.eslintrc.json
.prettierrc
tsconfig.json
...

But, recently I've deployed a new version of the app and the serverless api (api\index.ts) stop working because it cannot find the module ../backend/const.ts.

The stack trace is the following:

2023-01-07T09:43:18.189Z    XXX ERROR   Error: Cannot find module '/var/task/backend/const' imported from /var/task/api/index.js
    at new NodeError (node:internal/errors:393:5)
    at finalizeResolution (node:internal/modules/esm/resolve:328:11)
    at moduleResolve (node:internal/modules/esm/resolve:965:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:909:12)
    at defaultResolve (node:internal/modules/esm/resolve:1173:79)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:841:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:76:40)
    at link (node:internal/modules/esm/module_job:75:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

In the index.ts to import all the needed constants and functions I'm using the notation import { a } from '../backend/const', but it appears that is not valid anymore.

  • I've already tried to move all the files inside the api folder (in a single file to not exceed the maximum api endpoint allowed) but it didn't work.
  • I've tried to update the references, but it didn't work.
  • I've tried to re-deploy everything, but it didn't work.
  • I've tried to change the node version (from 16 -> 18), but it didn't work.
  • I've tried to change the target in tsconfig.json from ESNext to ES2019, but it didn't work.

The weird thing is that it works in the local environment but not in prod.

Does somebody have some ideas to fix or debug this problem?

CodePudding user response:

See https://github.com/vercel/community/discussions/1225

For now, you can set VERCEL_CLI_VERSION as environment variable in your Vercel Project to [email protected].

  • Related