Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'request'/'form-data' #5

Closed
morphatic opened this issue Nov 13, 2016 · 5 comments
Closed

Cannot find module 'request'/'form-data' #5

morphatic opened this issue Nov 13, 2016 · 5 comments

Comments

@morphatic
Copy link

The Problem

I'm building a package that depends on request-promise-native. When I try to compile, I'm getting the following errors on the console:

6 import request = require('request');
                           ~~~~~~~~~

node_modules/@types/request-promise-native/index.d.ts(6,26): error TS2307: Cannot find module 'request'.


15 import FormData = require('form-data');
                             ~~~~~~~~~~~

node_modules/@types/request/index.d.ts(15,27): error TS2307: Cannot find module 'form-data'.

I can't figure out why this is happening or how to make these errors go away.

Implementation Details

Here are the relevant bits from package.json:

{
  "devDependencies": {
    "@types/request": "0.0.33",
    "@types/request-promise-native": "^1.0.2"
  },
  "dependencies": {
    "request": "^2.78.0",
    "request-promise-native": "^1.0.3"
  }
}

Here is my tsconfig.json:

{
    "compilerOptions": {
        "module": "es6",
        "target": "es6",
        "declaration": true,
        "declarationDir": "dist",
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true,
        "noImplicitReturns": true,
        "pretty": true,
        "removeComments": true
    },
    "include": [
        "src/**/*.ts"
    ]
}

And here is an example of how I'm using rpn in my code:

let rp = require('request-promise-native');

interface MyInterface {
    // interface definitinon...
}

class MyClass {
    static async getSomething(param: string): Promise<MyInterface> {
        return await rp({
            uri: "http://placetogetsomething.com",
            qs: { param: param },
            json: true
        }).then(
            (result: any): MyInterface => result,
            (error:  any): MyInterface => { throw Error(error.error_message); }
        );
    }
}

I'm positive that both the request and form-data packages are installed in node_modules. Their type definitions are also installed in node_modules/@types. FWIW, when I tried this with request-promise I also got an error saying that Bluebird was not being found. I'm under the impression that @andy-ms and others from Microsoft are maintaining the type definitions for this package, so I'm a little puzzled as to why I'm getting this error. FYI, I'm using typescript v2.0.9. Thanks!

@analog-nico
Copy link
Member

@friedemannsommer Since you are watching request-promise and use TypeScript I wonder if you could say something about this issue? Btw, request-promise-native is designed the exact same way as request-promise in regards to its dependencies, file locations etc. Thanks for helping out!

I am very sorry that I can't help. I never used TypeScript before. But I googled "TS2307" and it seems you may fix it by changing the search paths and working directory of your TypeScript compiler.

@friedemannsommer
Copy link

Hi @morphatic you need to update your tsconfig.json that it looks like this:

The property "moduleResolution" was missing in your tsconfig.json. If you want to use modules from the node_modules directory you need to add the property "moduleResolution" with the value "node".

For more Information about TypeScript module resolution checkout the docs: https://www.typescriptlang.org/docs/handbook/module-resolution.html

{
    "compilerOptions": {
        "module": "es6",
        "target": "es6",
        "declarationDir": "dist",
        "moduleResolution": "node",
        "pretty": true,
        "declaration": true,
        "noImplicitAny": true,
        "removeComments": true,
        "noImplicitReturns": true,
        "suppressImplicitAnyIndexErrors": true
    },
    "include": [
        "src/**/*.ts"
    ]
}

@ghost
Copy link

ghost commented Nov 14, 2016

This issue is microsoft/TypeScript#11103. This is broken in 2.0.9, but is fixed in 2.1.1.

Also:

  • We don't directly maintain the over 2300 packages on DefinitelyTyped. They are user-contributed. We do work to help automate the process and review new packages.
  • When a node_modules/@types/foo package is present, the TypeScript compiler will ignore node_modules/foo; it resolves imports to "foo" with node_modules/@types/foo/index.d.ts instead. So, any compile (as opposed to run-time) errors are the fault of the @types package.

@analog-nico
Copy link
Member

@friedemannsommer Thanks for helping out!

@andy-ms Thanks for your info. I'll keep that in mind for the next time a TypeScript-related issue is opened!

@morphatic Does this fix your issue?

@morphatic
Copy link
Author

Yes! This fixed it. It also solves a HUGE mystery that I've been wrestling with for two weeks. TYVM!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants