Deploying to Payload

default discord avatar
Josh828
3 weeks ago
12

Hello everyone! I'm hoping that my issue here is something very simple that I overlooked. I am attempting to deploy a PayloadCMS application to Payload Cloud. I have deployed various Payload applications before, and have had no issues. Today, I attempted to deploy the same way that I always have, but I keep getting the following error:



Required ENV variables


Your payload.init() function must use MONGODB_URI and PAYLOAD_SECRET variables. Payload Cloud provides these for you. Ensure your spelling is correct.



I am using a Payload defined MongoDB, and I have ensured that the SECRET_KEY is not empty. This issue is occuring while deploying with PayloadCMS version 2.0.15. I have followed the error message instructions and added the SECRET_KEY and MONGODB_URI env variables to my payload.init() function with no luck. Please let me know if more information is needed to address this issue.



This is where MONGODB_URI is used in the payload.config.ts file



This is the entire server.ts file. I did try plugging in the MONGODB_URI to the payload.init() file, but I was getting an error when I did that.

  • discord user avatar
    denolfe
    Payload Team
    3 weeks ago

    I can look into this. Can you provide your project ID from your project Settings -> Billing?

  • default discord avatar
    Josh828
    3 weeks ago

    Yes, this is the Project ID:


    654a8041a24033b20bf22fe6

  • discord user avatar
    denolfe
    Payload Team
    3 weeks ago

    Hmm, odd that it isn't showing you logs on the project page. Here is what I see on the backend.



    [2023-11-07 18:32:41] │ Running custom build command: yarn build
    [2023-11-07 18:32:43] │ yarn run v1.22.19
    [2023-11-07 18:32:43] │ $ yarn copyfiles && yarn build:payload && yarn build:server
    [2023-11-07 18:32:45] │ $ copyfiles -u 1 "src/**/*.{html,css,scss,ttf,woff,woff2,eot,svg,jpg,png}" dist/
    [2023-11-07 18:32:46] │ /workspace/node_modules/cliui/build/index.cjs:291
    [2023-11-07 18:32:46] │ const stringWidth = require('string-width');
    [2023-11-07 18:32:46] │                     ^
    [2023-11-07 18:32:46] │ 
    [2023-11-07 18:32:46] │ Error [ERR_REQUIRE_ESM]: require() of ES Module /workspace/node_modules/string-width/index.js from /workspace/node_modules/cliui/build/index.cjs not supported.
    [2023-11-07 18:32:46] │ Instead change the require of index.js in /workspace/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/cliui/build/index.cjs:291:21)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/yargs/build/index.cjs:2861:12)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/yargs/index.cjs:5:30)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/copyfiles/copyfiles:4:12) {
    [2023-11-07 18:32:46] │   code: 'ERR_REQUIRE_ESM'
    [2023-11-07 18:32:46] │ }
    [2023-11-07 18:32:46] │ error Command failed with exit code 1.
    [2023-11-07 18:32:46] │ info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    [2023-11-07 18:32:46] │ error Command failed with exit code 1.
    [2023-11-07 18:32:46] │ info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    [2023-11-07 18:32:46] │ building: exit status 1


    Looks like you're possibly trying to use an ESM-only module. Is that possible? Does

    build

    run successfully when ran locally?

  • default discord avatar
    Josh828
    3 weeks ago

    To my knowledge, I shouldn't have an ESM-only module. I'll double check that now. The 'yarn build' command does run successfully in a local environment. I ended up creating a new payload app using npx create-payload-app and recreating the codebase from scratch. That also only worked in a local environment. I'm not using any dependencies other than those that come with Payload.



    Is the cliui node_module included in the payload dependency? The screenshot shows the dependencies that are being used, yet the cliui module keeps getting added to my yarn.lock file when I run yarn install. Based on build logs that you shared, that module is causing the issue.



    We have other apps hosted on Payload that work and have the cliui module installed. That makes me wonder if the issue is coming from the error shown in screenshot. If so, I'm not sure how I would fix this since I am using both of the env variables depicted.

    image.png
    image.png
  • discord user avatar
    denolfe
    Payload Team
    3 weeks ago

    I think the troubleshooting help is misleading in this case



    [2023-11-07 18:32:46] │ Error [ERR_REQUIRE_ESM]: require() of ES Module /workspace/node_modules/string-width/index.js from /workspace/node_modules/cliui/build/index.cjs not supported.
    [2023-11-07 18:32:46] │ Instead change the require of index.js in /workspace/node_modules/cliui/build/index.cjs to a dynamic import() which is available in all CommonJS modules.
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/cliui/build/index.cjs:291:21)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/yargs/build/index.cjs:2861:12)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/yargs/index.cjs:5:30)
    [2023-11-07 18:32:46] │     at Object.<anonymous> (/workspace/node_modules/copyfiles/copyfiles:4:12) {
    [2023-11-07 18:32:46] │   code: 'ERR_REQUIRE_ESM'
    [2023-11-07 18:32:46] │ }


    This block indicates that you are using a module called

    string-width

    , and the version you're using is ESM-only.



    Can you check what version of that dependency you're running?

    yarn why string-width


    From looking at their code, that package has been ESM-only for quite a few versions

  • default discord avatar
    Josh828
    3 weeks ago

    Its running v5.1.2



    That's the version that was installed with the payload cloud package

    image.png
  • discord user avatar
    denolfe
    Payload Team
    3 weeks ago

    Thanks, let me look into this

  • default discord avatar
    Josh828
    3 weeks ago

    Of course, thank you for looking into this issue.

  • discord user avatar
    denolfe
    Payload Team
    2 weeks ago

    @Josh828 Trying to recreate this now. Can you get me your exact versions of payload and all payload packages? Are you running yarn w/ workspaces by any chance?



    I haven't been able to recreate this. Any extra info would be helpful.

  • default discord avatar
    Josh828
    2 weeks ago

    I apologize for the delayed response. I tried with both versions 2.0.0 and 2.0.15 and got the same result. The screenshot of the dependencies show the exact packages that were used during the deployment attempt. Yes, yarn was used to install the dependencies. I also tried using npm to see if that would give a different result, which it didn't.



    Unfortunately that is all of the information that my team has on the issue. We spun up a barebones Payload project and attempted to deploy that as well. That ended with the same result.



    Steps taken:


    1) ran npx create-payload-app@latest to create a new Payload application


    2) Added database requirements in order to connect to MongoDB


    3) Deployed to Payload



    Hence, we were not able to deploy the barebones Payload application to Payload CLoud.

  • default discord avatar
    oldmanpewpew
    2 weeks ago

    RankedDev28 - two things earlier that got me.



    1. the blank payload app doesn't have yarn as a dependency, but yarn is required as part of teh build process


    2. on payload cloud, it by default wants to do npm run start (which doesn't exist in the blank payload app either), instead of npm run serve (a config option)

  • discord user avatar
    denolfe
    Payload Team
    2 weeks ago

    Yarn will never be a dependency of a project, since it is a global tool.



    create-payload-app should intelligently detect if you have yarn on your system and generate the template accordingly (optionally there are

    --use-yarn

    ,

    --use-npm

    , and

    --use-pnpm

    flags).



    If you create a project from the cloud interface, it will fill out the build command, start command, etc for you.



    Sounds like you did create-payload-app locally, then did an import when creating your cloud project. There is no way for us to detect what your commands are dynamically, unfortunately. Something we could improve is the expansion of those options to make it more obvious that they need to be tweaked. Right now it is collapsed by default.



    Circling back on the ESM issue initially by @Josh828 . Issue reproduced, looking into solution now. Being tracked here:

    https://github.com/payloadcms/payload/issues/4109


    A workaround has been added to that issue. Add the following to your package.json:



    "resolutions": {
      "jackspeak": "2.1.1"
    }
Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.