#importConfig failing with ERR_INVALID_URL

17 messages · Page 1 of 1 (latest)

steep trail
#

I've started with the provided /scripts/standalone-script.js as a guide. Whatever I do, importConfig fails with code: 'ERR_INVALID_URL'. I've also tried placing the import file in the same directory but. still the same result. Using v3.0 beta.

const payloadConfigPath = "./stripePaymentLinks.ts";
const co = importConfig(payloadConfigPath);

input: 'undefined',
code: 'ERR_INVALID_URL'
}
⨯ unhandledRejection: TypeError [ERR_INVALID_URL]: Invalid URL

rapid hollowBOT
hexed heron
#

Odd

#

What example are you following @steep trail

#

Have you tried resolving your path with the path module

#
        let payloadConfigPath = path.resolve(projectDir, 'src/payload.config.ts')
#

or something like that

steep trail
# hexed heron What example are you following <@439062699188551691>

This file was provided with the Payload Beta. This is what I have been using.

/**

  • This is an example of a standalone script that loads in the Payload config
  • and uses the Payload Local API to query the database.
    */

import { getPayload } from 'payload'
import { importConfig } from 'payload/node'

async function run() {
const awaitedConfig = await importConfig('../../payload.config.ts')
const payload = await getPayload({ config: awaitedConfig })

const pages = await payload.find({
collection: 'pages',
})

console.log(pages)
process.exit(0)
}

run().catch(console.error)

hexed heron
#

hmm

#

Outside of the run function, i would pre-define the path

#

const examplePath = path.resolve(........)

#

then const awaitedConfig = await importConfig(examplePath)

#

It could be that the directory structure has changed, or some other oddity I'm not sure about

#

Let me know if that works

steep trail
# hexed heron Let me know if that works

Thanks for your help. Using path.resolve seems to have fixed that issue. I'm not getting a MODULE_NOT_FOUND error, which probably relates to the content of my config file I'm importing. Will continue to investigate

rapid hollowBOT
steep trail
#

I wanted to reply to my own post. I was unable to solve the MODULE_NOT_FOUND error whatever I tried. However, I did find an alternative way of dealing with the problem that does work.

The sample file in /scripts/standalone-script.js showed importing the config with these two lines:

const awaitedConfig = await importConfig('../../payload.config.ts')
const payload = await getPayload({ config: awaitedConfig })

Which always failed (except when using the additional path.resolve function suggested above, which changed the issue from ERR_INVALID_URL to MODULE_NOT_FOUND.

In this issue (https://github.com/payloadcms/payload-3.0-demo/issues/199) there is a mention of obtaining the config in a different way:

import configPromise from '@payload-config';

Then:

const payload = await getPayload({ config: configPromise });

Using this method worked for me. That's a day and a half I won't get back! 😄

GitHub

Should it be possible to do something like this from a server component? import React from 'react'; import configPromise from '@payload-config'; import { getPayload, type Payload } ...