Skip to content

"Cannot find module" message should be more explicit, saying where it's looking #5149

Open

Description

With all the vagaries around module loading, it'd be great to give a more verbose, helpful error message for "Cannot find module 'foo'", saying where the compiler looked for the module. That can help clue the user into (1) how TypeScript works and (2) how to fix their issue.

The message text could be semi-static, maybe something like: IF using commonjs loading AND path isn't relative THEN say "Couldn't find module Foo; searched node_modules directory off directory XXXX and its ancestors and the package.json Typings path YYYY.

Better doc around the module loading algorithm helps, but just putting some of that info in the error message makes it very easily discovered. It also helps clue the user into what isn't the problem (e.g. the message above confirms that using commonjs module loading & found Typings in package.json, but something else seems to be the problem).

Activity

  1. mhegazy commented on Oct 21, 2015

    @mhegazy
    Contributor

    Would love to see a proposal/PR here. one constraint is it needs to work with the language service as well as with the compiler. the failed lookup locations are cached on the file in case of the compiler but not the language service. either ways any help would be appreciated.

  2. BretJohnson commented on Oct 21, 2015

    @BretJohnson
    Author

    Could someone maybe document here, or point to existing doc elsewhere, on the rules for where it looks (at least in the commonjs case). That could be a good first step in turning those rules into a reasonably concise message. I can help with the 2nd part, but less so with the 1st. Thx.

  3. mhegazy commented on Oct 21, 2015

    @mhegazy
    Contributor

    The module resolvers will return a list of ResolvedModuleWithFailedLookupLocations objects, each has a file path and a set of locations where lookup failed, here is the definition: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts#L2327

    Here is the resolver for node modules, you can see how the failed lookup locations are populated: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/program.ts#L50

    The error is actually reported later on in the checker when the resolution of the module fails: https://github.com/Microsoft/TypeScript/blob/master/src/compiler/checker.ts#L1013

    Vladimir Matveev (@vladima) can advice on the best way to tackle the the language service part

  4. DanielRosenwasser commented on Oct 21, 2015

    @DanielRosenwasser
    Member

    We should consider a --verboseResolution option here.

  5. mhegazy commented on Oct 23, 2015

    @mhegazy
    Contributor

    We talked about with before. +1 for that

  6. BretJohnson commented on Oct 24, 2015

    @BretJohnson
    Author

    The module loading rules should also be documented (in precise English) in the TypeScript wiki/handbook, right? And we don't really have that doc currently, correct? Can someone on the TypeScript team fill that in. Thanks.

  7. mhegazy commented on Oct 28, 2015

    @mhegazy
    Contributor

    That is correct. we need to add some more documentation on resolving references and imports. i logged an issue a while back: microsoft/TypeScript-Handbook#68

    PRs for documentation, partial be it, are definitely appreciate it.

    Vladimir Matveev (@vladima) or myself can answer any specific questions you would have about how resolution work.

  8. BretJohnson commented on Mar 22, 2016

    @BretJohnson
    Author

    FWIW, I'd still love to see this doc & feature. Have the module loading rules been documented yet?

  9. vladima commented on Mar 22, 2016

    @vladima
    Contributor

    With nightly builds you can use traceModuleResolution option to enable verbose logging of module resolution process.

  10. mhegazy commented on Mar 22, 2016

    @mhegazy
    Contributor

    I have added some documentation to the handbook about module resolution: https://github.com/Microsoft/TypeScript-Handbook/blob/release-2.0/pages/Module%20Resolution.md.

    Check it out, and let me know if there is gaps that you would like to see filled.

  11. 1241282015 commented on Feb 21, 2017

    @1241282015

    I think you can write xx.d.ts file, within that you can write declare clause, and use reference in your xx.ts file. And you can read: http://www.typescriptlang.org/docs/handbook/modules.html#ambient-modules

  12. added this to the milestone on Jan 4, 2018
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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions