Skip to content

question: No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies. #261

Closed

Description

@viktor-ku

Hi. Can anyone describe what exactly is this?

I have like 4 of those messages

Activity

  1. DavidBabel commented on Sep 22, 2018

    @DavidBabel

    Same issue for me, i don't exactly get why it prints

  2. added
    type: question
    on Nov 4, 2018
  3. NoNameProvided commented on Nov 4, 2018

    @NoNameProvided
    Member

    Hi @viktor-ku and @DavidBabel!

    What enviroment do you use class-validator? What other libraries do you use with it?

    The reason to this error as the message states there are multiple class-validator versions in your node_modules folder and they do not share the same metadata storage. Let me explain in details below:

    Reason

    Why do I see this

    First, historyically class-validator only checked the type of known properties when validating a class and this led to silent errors when no metadata was found for a class and class-validator validated nothing and stayed silent. So an option was added to throw an error on unknown classes and the above message is logged when that option is not enabled to warn you.

    Technical reason

    To understand the problem, first, we need to understand how class-validator knows what to check on the properties. It knows ther required types because of the attached decorators, which will register the expected type in an internal storage inside class-validator.

    But that storage is not shared accross the different versions of class-validator, so if you declarad a semver range in your modules the way that they doesn't share the same allowed latest version then npm will install multiple versions for you and will break it.

    So this is what propably is in your node_modules right now:

    node_modules
      /class-validator@0.9.x
      /your-another-lib-or-something-else-using-class-validator (requires 0.8.x)
        /node_modules
    	  /class-validator@0.8.x
      / ... lot of other folders
    package.json (requires 0.9.x)
    

    In the example above the module called your-another-lib-or-something-else-using-class-validator requires a different version of the class-validator than the main project, so npm will install that specific version for him, but that is our problem, because now if you want to validate any model defined in that module from the outside you will get the error because your outer proram will use class-validator@0.9.x while the inner program used the inner class-validator@0.9.y instance to register it's type metadata.

    So the folder sturcutur we want is the following:

    node_modules
      /class-validator@0.9.x
      /your-another-lib-or-something-else-using-class-validator (requires version 0.9.x)
      / ... lot of other folders
    package.json (requires 0.9.x)
    

    Solution

    In short, you need to make sure to install the same class-validator accross all the projects you use together. This is most easily done by adding the same semver range, eg: 0.9.x. (You cannot use ^0.9.0 because we have not reached 1.0, so a minor change considered a major change by semver.)

    Required extra steps

    You need to update npm to the latest version, npm flatten the node_modules for you sice the 6. version only.

    I hope I could explain the probem (this one is hard to explain!), if anything is not clear, feel free to ask more, and I will try my best to explain it.

  4. added
    status: awaiting answer
    on Nov 4, 2018
  5. NoNameProvided commented on Nov 4, 2018

    @NoNameProvided
    Member

    This is related to #132.

  6. NoNameProvided commented on Nov 4, 2018

    @NoNameProvided
    Member

    An another explanation: #181 (comment)

  7. FSM1 commented on Nov 8, 2018

    @FSM1

    @NoNameProvided I am also experiencing these errors and unable to validate. I have ensured that there is only one version of class-validator installed throughout my package tree. I have confirmed by searching the node_modules folder that only one version has been installed, and the node_modules has been flattened.

    image

    I still unfortunately get the error in my console and am unable to validate.

    Thanks in advance.

  8. niekvb commented on Nov 21, 2018

    @niekvb

    Faced the same problem, this helped for me:

    Instead of declaring a variable with the type of the class

    const user: User = {
        first: "John",
        last: "Doe",
    };

    Initialize the class first before assigning data

    const user = User();
    user.first = "John";
    user.last = "Doe";

    It might not be the most beautiful way to solve it, but it seems to work.

    Let me know if this solves your issue.

  9. grbatinic-bogdan commented on Nov 25, 2018

    @grbatinic-bogdan

    I'm having the same issue. In my project I have routing-controllers package which currently has outdated dependency of class-validator package. There's an open ticket for this as well.

    I would argue that routing-controllers shouldn't have a hard dependency on class-validator package, because of the nature of this issue.

  10. diegolaciar commented on Nov 29, 2018

    @diegolaciar

    @NoNameProvided,

    I have the same issue, I get the warning and the validate don' t return any ValidationError.

    I also I have confirmed by searching the node_modules folder that only one version has been installed, and the node_modules has been flattened.

    npm v6.4.1

    tried the command
    npm dedupe
    but no luck solving this issue.

    thanks

  11. josephbuchma commented on Dec 2, 2018

    @josephbuchma

    Having same issue, node_modules flattened 😞

    @niekvb check out this

  12. josephbuchma commented on Dec 2, 2018

    @josephbuchma

    Ok, I got it working! For some reason it does not work when used with typedi container.
    To be more specific, it worked after I removed following piece:

    // ...
    import { Container } from 'typedi';
    import * as ClassValidator from 'class-validator';
    
    ClassValidator.useContainer(Container);
    
    // ...
  13. RDeluxe commented on Jan 15, 2019

    @RDeluxe

    Thanks, I had the same issue.
    As we are using Lerna, the flattening is not an effective solution.
    Let's say if have my own 2 packages, A and B, both having class-validator as a dependency.
    Then, I add an external dependency, called C, to my B package. C also requires class-validator, but with another version.

    Then, using lerna boostrap all the dependecies are installed in A/node_modules, B/node_modues, etc.
    The last step lerna is taking, if you require local package in the same monorepo, is to create a symlink like A/node_modules/B -> ../../B. That's when you lose any flattened structure, and you end up with a different version of class-validator (as far as I can tell).

    Setting the same version of class-validator used in C in my packages in both A and B solved the issue

  14. RDeluxe commented on Feb 6, 2019

    @RDeluxe

    Hello again.

    Seems that even with one single version of class-validator, the issue still exists. Any indications about why this could be the case ?

    Edit:

    Ok, after 2 hours trying to fix this, we realized that Typedi was not going to help (we did not manage to make it work).

    Our use case is really simple: we use lerna. And as such, we don't have a flattened 'node_modules' during development.

    class-validator should really support such a use case out of the box

  15. 44 remaining items

  16. satanTime commented on Mar 31, 2020

    @satanTime
    Contributor

    @rfgamaral, I've not seen it in your repo. let's go back to the "chat" :)

  17. rfgamaral commented on Mar 31, 2020

    @rfgamaral

    Sorry everyone, and sorry @satanTime for wasting your time. There was no problem, I made a bit mistake (kinda tired) and it was my problem all along.

    Everything is great, please keep up the good work :)

  18. vlapo commented on Mar 31, 2020

    @vlapo
    Contributor

    I hate this misleading warn message but for now it is our only indication about missing metadata in prod (whatever it is because of no validation rules or more than one class-validator libs installed).
    Lets to nothing now. We may change/remove this warn in the future

  19. vlapo commented on Mar 31, 2020

    @vlapo
    Contributor

    Thanks @satanTime for your quick reactions :)

  20. kctang commented on Apr 1, 2020

    @kctang

    Hi guys, sharing my experience on this...stuck for 2-3 days, now I got a solution that words for my scenario.

    I got the ~ error message when trying to validate models via Jest (unit test) in a NestJS project. The NestJS project references models from a Lerna monorepo (relative path - and NestJS is not part of the monorepo).

    Jest references packages in the monorepo via moduleNameMapper like:

      "moduleNameMapper": {
        "^@my-lerna/(.*)$": "<rootDir>/../lib/packages/$1/src/index.ts"
      }
    

    I think, the reason I am getting the error message is because the model files resolve 'class-validator' by traversing up its directory path. So, even when if i hoist dependencies to the root Lerna directory, it does not work.

    Solution: Specifically, tell Jest know how to resolve 'class-validator' by adding an entry to moduleNameMapper (a Jest configuration option):

      "moduleNameMapper": {
        "class-validator": "<rootDir>/node_modules/class-validator",
        "^@my-lerna/(.*)$": "<rootDir>/../lib/packages/$1/src/index.ts"
      }
    

    Hope this helps someone with similar scenario.

  21. satanTime commented on Apr 5, 2020

    @satanTime
    Contributor

    Hello to your house too, @Syy0n.

    Steps to reproduction please.

  22. glikaj commented on Apr 8, 2020

    @glikaj

    For me it happens in authorizationChecker @satanTime.

    const expressApp: Application = createExpressServer({
      //..
      authorizationChecker: async (action: Action, roles: string[]) => {
        const user = await connection.getMongoRepository(User).find();
        console.log(user);
        return true;
      },
      //...
    }); 

    This happens me when i call the User model or any other class that imports the 'class-validator'.

  23. satanTime commented on Apr 8, 2020

    @satanTime
    Contributor

    Hi @glikaj, the first thing is please verify you use 0.12.0-rc.0 and there's not any lib that uses another version. If the node_modules have only 0.12.0-rc.0 then it means there's no any validation rule and the case is similar to @rfgamaral had - just add a validation rule somewhere and the error should gone. Other cases I would say aren't possible, but I believe in magic and would like to ask you to share the source code for an investigation :)

  24. glikaj commented on Apr 9, 2020

    @glikaj

    Thank you @satanTime, update to 0.12.0-rc.0 fixed the problem.

  25. lock commented on Apr 17, 2020

    @lock

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  26. locked as resolved and limited conversation to collaborators on Apr 17, 2020
  27. added
    status: done/released
    and removed
    status: awaiting answer
    on Aug 8, 2020
  28. changed the title [-]No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/-] [+]question: No metadata found. There is more than once class-validator version installed probably. You need to flatten your dependencies.[/+] on Aug 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: done/releasedtype: question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions