Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependency issue using localAPI in a sveltekit application #6447

Open
Snailedlt opened this issue May 21, 2024 · 3 comments
Open

dependency issue using localAPI in a sveltekit application #6447

Snailedlt opened this issue May 21, 2024 · 3 comments
Assignees
Labels
status: awaiting-reply status: needs-triage Possible bug which hasn't been reproduced yet v3

Comments

@Snailedlt
Copy link
Contributor

Snailedlt commented May 21, 2024

Link to reproduction

https://sveltekit-payload-frontend.vercel.app/

Describe the Bug

I get some dependency issues when using the localAPI in a Sveltekit application. I tried using it like so:

// payload-client.ts
import { type BasePayload, getPayload, type GeneratedTypes } from 'payload';
import { importConfig } from 'payload/node';
import 'dotenv/config';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

let payloadInstance: BasePayload<GeneratedTypes>;

// payload-client.ts
export async function initializePayload() {
	const __dirname = path.dirname(fileURLToPath(import.meta.url));
	const payloadConfigPath = path.join(__dirname, './payload/payload.config.ts');
	const awaitedConfig = await importConfig(payloadConfigPath);

	payloadInstance = await getPayload({ config: awaitedConfig });
}

export async function getPayloadInstance() {
	if (!payloadInstance) {
		throw new Error('Payload has not been initialized. Call initializePayload first.');
	}

	return payloadInstance;
}
// +page.server.ts
import { getPayloadInstance, initializePayload } from '$lib/payload-client';

export async function load() {
	await initializePayload();
	const payloadInstance = await getPayloadInstance();
	const user = await payloadInstance.findByID({
		collection: 'users',
		id: '1'
	});

	return { msg: 'welcome', user: user };
}

Running it locally works just fine, both using pnpm dev and making a production build with pnpm build pnpm serve... however deploying it to vercel gives the following error:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/.pnpm/payload@3.0.0-beta.35_@swc+core@1.5.7_@swc+helpers@0.5.11__@swc+types@0.1.7_graphql@16.8.1_typescript@5.4.5/node_modules/payload/dist/bin/loader/index.js' imported from /var/task/.svelte-kit/output/server/entries/pages/payload/payload.config.ts
    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:1161:14)
    at defaultResolve (node:internal/modules/esm/resolve:1204:79)
    at nextResolve (node:internal/modules/esm/hooks:866:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:304:30)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:35)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:322:34)
    at Hooks.register (node:internal/modules/esm/hooks:165:51) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///var/task/node_modules/.pnpm/payload@3.0.0-beta.35_@swc+core@1.5.7_@swc+helpers@0.5.11__@swc+types@0.1.7_graphql@16.8.1_typescript@5.4.5/node_modules/payload/dist/bin/loader/index.js'
}

I tried both inside a turborepo and in a separate project without turborepo. Both failed with similar errors. The error above is from the non-turborepo version, and this error is from the turborepo:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/node_modules/.pnpm/payload@3.0.0-beta.32_@swc+core@1.5.7_@swc+types@0.1.7_graphql@16.8.1_typescript@5.4.5/node_modules/payload/dist/bin/loader/index.js' imported from /var/task/apps/web/.svelte-kit/output/cms/src/payload.config.ts
    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:1161:14)
    at defaultResolve (node:internal/modules/esm/resolve:1204:79)
    at nextResolve (node:internal/modules/esm/hooks:866:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:304:30)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:35)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:234:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:322:34)
    at Hooks.register (node:internal/modules/esm/hooks:165:51) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///var/task/node_modules/.pnpm/payload@3.0.0-beta.32_@swc+core@1.5.7_@swc+types@0.1.7_graphql@16.8.1_typescript@5.4.5/node_modules/payload/dist/bin/loader/index.js'
}

Before I opened this issue I discussed it on Discord. The linked discord thread can be found here: https://discord.com/channels/967097582721572934/1238608849099948212

To Reproduce

In order to test the turborepo version:

  1. Fork my repo: https://github.com/Snailedlt/svelte-payload-example
  2. Pull down the fork locally
  3. Copy the .env.example file cp .env.example .env
  4. Run pnpm i and pnpm dev to start the cms and web projects. Create a new user in Payload (http://localhost:3000)
  5. Go to the frontend (http://localhost:5173) and notice how your email is displayed at the bottom of the page similar to this:
    image
  6. If you wanna test the production build you can run pnpm build and pnpm start. This should work the same if done locally.
  7. Now that we know it works locally we can test in vercel, but first we need a database. Create a hosted postgres database. I use neon for this: https://neon.tech/ (login link: https://console.neon.tech/app/projects)
  8. Deploy both the cms app and the web app as two separate vercel deployments.
    • Here's how the cms app should be deployed:
      image
      Replace the DATABASE_URI env with your connection string. If you're using neon that can be found in your dashboard. Make sure it's a pooled connection:
      image
    • Here's how the web app should be deployed:
      image
      Remember to add the env vars here too.
  9. Once it's all deployed it should all be green, but if you visit the url for your frontend (web), it should show a 500 internal error. And if you go to the logs in vercel you should be able to see the same error message as described in this issue.

If you want to test the simplified non-turborepo version:

  1. Follow all the steps from the turborepo version
  2. fork my repo: https://github.com/Snailedlt/sveltekit-payload-frontend
  3. If you wanna test it locally it's just cp .env.example .env, pnpm i and pnpm dev
  4. deploy it to vercel using the default SvelteKit (v1) settings. Use the same DATABASE_URI as in the turborepo example
  5. Go to the deployed website and notice that you get a 500 error here too. If you go to logs in vercel should see the same errors as shown in this issue.

Payload Version

beta (3.0.0-beta.34 as of the time this issue was posted)

Adapters and Plugins

db-postgres, lexical

@Snailedlt Snailedlt added the status: needs-triage Possible bug which hasn't been reproduced yet label May 21, 2024
@denolfe denolfe added the v3 label May 22, 2024
@JarrodMFlesch
Copy link
Contributor

@Snailedlt my gut tells me that this is a limitation from Vercel. If you check out their docs related to root directory, they say that you cannot access files outside the root of your app.

It looks like you are trying to go up and out of your web app root dir and into another folder within your mono repo in this file.

Could you try copying the payload config into your web app directory and adjust your imports to go to that config file to test this theory?

@Snailedlt
Copy link
Contributor Author

@JarrodMFlesch Thanks for the response :)

they say that you cannot access files outside the root of your app.

Looks like it should have access to the files outside of the root:
image
I actually got an error about the file path to the config being wrong. After correcting it, it worked even though the file path is outside of the root of the app.

Could you try copying the payload config into your web app directory and adjust your imports to go to that config file to test this theory?

This is what I've done in the other repo I linked to at the bottom of the "To reproduce" section:

If you want to test the simplified non-turborepo version:

Follow all the steps from the turborepo version
fork my repo: https://github.com/Snailedlt/sveltekit-payload-frontend
If you wanna test it locally it's just cp .env.example .env, pnpm i and pnpm dev
deploy it to vercel using the default SvelteKit (v1) settings. Use the same DATABASE_URI as in the turborepo example
Go to the deployed website and notice that you get a 500 error here too. If you go to logs in vercel should see the same errors as shown in this issue.

@Snailedlt
Copy link
Contributor Author

@AlessioGr let me know if you need anything, whether it be clarification or help testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting-reply status: needs-triage Possible bug which hasn't been reproduced yet v3
Projects
None yet
Development

No branches or pull requests

4 participants