Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

TypeError: Cannot read properties of undefined-if(script.startsWith('migrate'))

default discord avatar
codebleidd2 years ago
11

Hey. I'm running into an error while trying to run

pnpm run payload

which is a script that runs a script :



"payload": "cross-env PAYLOAD_CONFIG_PATH=payload.config.ts payload"

What I'm trying to o is a get a migration because I removed a column from a Block. I use MongoDB database. Let me know if you need any other information.



I'm getting an error:


> @ payload /nnn/app
> cross-env PAYLOAD_CONFIG_PATH=payload.config.ts payload

/nnn/app/node_modules/.pnpm/payload@2.9.0_@swc+helpers@0.5.3_@types+react@18.2.51_esbuild@0.19.12_typescript@5.3.3_webpack@5.90.1/node_modules/payload/dist/bin/index.js:55
if (script.startsWith('migrate')) {
           ^

TypeError: Cannot read properties of undefined (reading 'startsWith')
    at Object.<anonymous> (/nnn/app/node_modules/.pnpm/payload@2.9.0_@swc+helpers@0.5.3_@types+react@18.2.51_esbuild@0.19.12_typescript@5.3.3_webpack@5.90.1/node_modules/payload/dist/bin/index.js:55:12)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/Users/maciekkolodziejczyk/Dev/bifor-final/node_modules/.pnpm/payload@2.9.0_@swc+helpers@0.5.3_@types+react@18.2.51_esbuild@0.19.12_typescript@5.3.3_webpack@5.90.1/node_modules/payload/bin.js:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

Node.js v18.18.0
 ELIFECYCLE  Command failed with exit code 1.     


Anyone had a similar problem while trying to run payload script?

  • discord user avatar
    denolfe
    2 years ago

    You need to pass another param as a command



    build

    or

    migrate

    etc



    A related fix will be in the next release, though

    https://github.com/payloadcms/payload/pull/4995
  • default discord avatar
    codebleidd2 years ago
    @967118574445547650

    thank you, it works 👍


    Just another question regarding that topic: is it possible to make payload create migration automatically based on collection and block schema changes?


    I tried

    payload migrate:create <name>

    but it just generates a migration file



    @967118574445547650

    Regarding possible question about migration prompt when starting dev server - it does not show up. This is my config and server, I use nodemon in dev modee



    Or is it possible only with postgres?

  • default discord avatar
    paulpopus2 years ago

    ignore me

  • discord user avatar
    denolfe
    2 years ago
    @1075330808761557062

    The new schema is automatically pushed when running in development.

  • default discord avatar
    codebleidd2 years ago
    @967118574445547650

    So when i change for example slug of a block, does payload recognise it somehow?


    I have changed block slug and move some field from array in a block to upper scope. Doesn't it require a migration? i dont want to lose data from it



    And what about prodcution?

  • default discord avatar
    paulpopus2 years ago

    For production you want to maintain a more strict approach to creating migrations and then running them as part of your deployment steps


    Think of it like versioning for the database schema



    With postgres (and most sql dbs) you

    will

    lose data when making schema changes unless you migrate that data as well, the automated migrations only handle schema, so you would have to add additional logic for the data itself, it just comes with the territory

  • default discord avatar
    codebleidd2 years ago
    @858693520012476436

    thank you for that, and sorry not to say that explicit: I use MongoDB right now and this is a DB I don't have an experience with comparing to postgres. So my questions are connected to Mongo.



    So you say I'll have to create a migration for mongo manually for some basic changes like renaming block?

  • default discord avatar
    paulpopus2 years ago

    Mongo is different, it's far more lenient for data schema so you can just make changes on the fly


    There will be inconsistencies with documents though and sometimes this can cause errors, since old docs wont have dead fields removed and this probably where you might want to do migrations in mongo



    but if you're just devving you most likely dont need to worry about it

  • default discord avatar
    codebleidd2 years ago
    @858693520012476436

    I have a site on prod right now where there is some data and wanted to change some stuff, ended up extending the schema and no error came up so far. It's a fairly fresh app so there is not much of a threat of something breaking up.


    If I understand correctly, there is no automatic helper/prompt in mongo that detects changes and provides some solution as it is for postgres, right?


    And if sth bigger happens, migration is a way to go, right? Is there any article you can recommend about how it can be done? Or just mongo docs?

  • default discord avatar
    paulpopus2 years ago

    yeah thats basically right, most of the time though this isnt an issue because schema changes dont happen

    that

    often and the areas where you may see errors is where the API expects certain data formats...for example if you change a

    content

    field from an array of blocks to richtext, then old content will error for you



    a migration script can be just a ts script file that uses the local API to modify data tbh



    yeah id look through the docs and other tutorials, i dont have any on hand to recommend

  • default discord avatar
    codebleidd2 years ago
    @858693520012476436

    Great, thank you very much for your help!

    :thankyou:
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.