Next.js Discord

Discord Forum

Do absolute imports work in Edge Functions?

Unanswered
Knopper gall posted this in #help-forum
Open in Discord
Knopper gallOP
Not using Next.js

Full repro: https://github.com/muradbuyukasik/lucia-drizzle-neon-example

I'm getting this error trying to deploy on Vercel:
Error: The Edge Function "api/auth/edge/signup" is referencing unsupported modules:
    - src/lib/auth.js: @/lib/db


tsconfig.json:
{
  "compilerOptions": {
    "target": "es2020",
    "module": "esnext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "baseUrl": "./src",
    "paths": {
      "@/*": ["*"]
    }
  }
}


signup.ts:
import { auth } from "@/lib/auth";

export const config = {
  runtime: "edge",
  regions: ["fra1"],
};

export default async function SignUp(
  request: Request & { body: { id: string; password: string } }
) {
  if (request.method !== "POST") return new Response("Must be POST");

  const body = await request.json();

  if (!body) return new Response("Must have body");
  if (!body.id || !body.password) return new Response("Missing parameters");

  try {
    const user = await auth.createUser({
      key: {
        providerId: "test",
        providerUserId: body.id,
        password: body.password,
      },
      attributes: {},
      userId: body.id,
    });

    return new Response(
      JSON.stringify({
        message: "Success! Created user.",
        user,
      })
    );
  } catch (error) {
    return new Response(JSON.stringify({ error: error }));
  }
}

2 Replies

Knopper gallOP
Removing the absolute import statements worked. The project builds and deploys seccessfully and I can hit the Edge endpoint successfully. However now when I hit a serverless endpoint I get the following error:

2023-07-13T14:42:47.240Z    undefined    ERROR    Error: Cannot find package '@/lib' imported from /var/task/src/api/auth/serverless/signup.js
    at new NodeError (node:internal/errors:399:5)
    at packageResolve (node:internal/modules/esm/resolve:894:9)
    at moduleResolve (node:internal/modules/esm/resolve:987:20)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:938:12)
    at defaultResolve (node:internal/modules/esm/resolve:1202:79)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
RequestId: 0a42b113-733c-4637-8b9c-dfbb281cc6cb Error: Runtime exited with error: exit status 1
Runtime.ExitError
Removing the absolute import statements from serverless functions as well:

2023-07-13T14:45:55.397Z    undefined    ERROR    Error: Cannot find module '/var/task/src/lib/auth' imported from /var/task/src/api/auth/serverless/signup.js
    at new NodeError (node:internal/errors:399:5)
    at finalizeResolution (node:internal/modules/esm/resolve:331:11)
    at moduleResolve (node:internal/modules/esm/resolve:994:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:938:12)
    at defaultResolve (node:internal/modules/esm/resolve:1202:79)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
    at link (node:internal/modules/esm/module_job:76:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
RequestId: 7fb6197f-6a33-491a-bc47-fc1aa263a1bb Error: Runtime exited with error: exit status 1
Runtime.ExitError