Next.js Discord

Discord Forum

Why can't i import my other file that contains some necessary functions?

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/src/lib/db' imported from /var/task/src/app/api/create-checkout-session/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:281:11)
    at moduleResolve (node:internal/modules/esm/resolve:866:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:990:14)
    at defaultResolve (node:internal/modules/esm/resolve:1033:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:685:12)
    at #cachedDefaultResolve (node:internal/modules/esm/loader:634:25)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:617:38)
    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:273:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:135:49) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///var/task/src/lib/db'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.

35 Replies

Dwarf Hotot
show code
@Dwarf Hotot show code
American black bearOP
import { VercelRequest, VercelResponse } from '@vercel/node';
import { queryStore, addPendingOrder, queryStock } from '../../db'
import { Store } from '../../store'
import { Product } from '../../product'
import { nanoid } from 'nanoid/non-secure'
import Stripe from 'stripe';



const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: '2025-04-30.basil',
});

interface order {
  product_id: number,
  quantity: number
}

interface POSTBODY {
  store_url: string,
  order: order[]
}

async function checkStockAvailability(order: order[], products: Product[]): Promise<boolean> {
  for (const item of order) {
    const product = products.find((p) => p.id === item.product_id);
    if (!product || product.stock === undefined || product.stock === null) {
      return false;
    }

    // Check if stock is available for the requested quantity
    if (product.stock < item.quantity) {
      return false;
    }
  }

  // If no issues were found, return null
  return true;
}

export default async function handler(req: VercelRequest, res: VercelResponse) {

  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

  if (req.method === 'OPTIONS') {
    return res.status(200).end(); // Stop here for preflight
  }

  try {
    const { store_url,order }: POSTBODY = req.body;

    const store: Store | null = await queryStore(store_url);
    if (!store) {
      return res.status(500).json({ error: 'No store found' });
    }

    const products: Product[] = await queryStock(store.id)
    console.log(products)
    if (!(await checkStockAvailability(order, products))) {
      return res.status(500).json({ error: 'No stock left or invalid order' });
    }  
}
Dwarf Hotot
/var/task/src/app/api/create-checkout-session/index.js
@Dwarf Hotot `/var/task/src/app/api/create-checkout-session/index.js`
American black bearOP
thats it yes, but as you can see it typescript actually yea
Dwarf Hotot
its okay node dont know ts, but /var/task/src/lib/db
@Dwarf Hotot its okay node dont know ts, but `/var/task/src/lib/db`
American black bearOP
that file is a total shit show, but here are its imports:
import { createClient } from "@libsql/client/web";
import { Store } from '../store'
import { nanoid } from 'nanoid/non-secure';


Store is just an interface
the rest would be just functions
Dwarf Hotot
is it exporting from ../../db and has queryStore, addPendingOrder, queryStock ?
American black bearOP
Yes. I was battling this "issue" that this thread is about for like 5 to 6 hours and i believe i tried every hypothesis i could come up with. Except for dynamic importing, but idk how relevant is that.
vercel.json:
{
    "functions": {
      "src/api/**/*": {
        "includeFiles": "src/db.ts"
      }
    }
  }
  
tsconfig.json:
{
    "compilerOptions": {

        "outDir": "dist",
        "baseUrl": "./",
        "paths": {
            "@/*": ["*"]
        },
      "module": "ESNext",
      "target": "ES2020",
      "moduleResolution": "node",
      "esModuleInterop": true,
      "forceConsistentCasingInFileNames": true,
      "strict": true,
      "skipLibCheck": true
    },
    "include": ["src/**/*"],
    "exclude": ["node_modules", "dist"]

}
  
@Dwarf Hotot is it exporting or not
American black bearOP
wdym by that to be more exact?
Dwarf Hotot
is it exporting from ../../db and has queryStore, addPendingOrder, queryStock ?
American black bearOP
yes
Dwarf Hotot
are you sure
American black bearOP
yes, ive tested all the functions locally.
Dwarf Hotot
okay then why you have vercel.json
American black bearOP
Chatgpt suggested me it as solution to this issue 😭

Anyways idk but isnt it necessary, when trying to host on vercel?
Dwarf Hotot
no
American black bearOP
uhh, when is it necessary or recommended to have then?
Dwarf Hotot
havent needed it in 4 years with next
American black bearOP
i dont have any framework on this project tho, since this is purely just to be an serverless backend
Dwarf Hotot
then remove that file and push
cuz looks like its breaking your path
American black bearOP
tried, still the same.
Cannot find module '/var/task/src/db' imported from /var/task/src/api/create-checkout-session/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:281:11)
    at moduleResolve (node:internal/modules/esm/resolve:866:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:990:14)
    at defaultResolve (node:internal/modules/esm/resolve:1033:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:685:12)
    at #cachedDefaultResolve (node:internal/modules/esm/loader:634:25)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:617:38)
    at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:273:38)
    at ModuleJob._link (node:internal/modules/esm/module_job:135:49) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///var/task/db'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
Dwarf Hotot
can you share a live share vscode link
American black bearOP
how D:
Dwarf Hotot
setup extension named Live Share and F1 > start collab..
@Dwarf Hotot setup extension named Live Share and F1 > start collab..
American black bearOP
have u requested to join it yet
Dwarf Hotot
dont make it readonly
American black bearOP
its a total mess/ass btw
Dwarf Hotot
Asian black bear
Please do not recommend people to screen share or coerce people into live share. It's an immense security risk - giving a stranger access to your machine is nothing you should do, ever. In addition it is against the spirit of this forum since there will be no history of what you actually did to potentially resolve the error.