1
votes

I'm trying to install a module named "request" https://github.com/request/request

in my angular 2 typescript project but I can't get it imported for some reason.

I tried installing the normal way with npm install --save request and I tried typings install request --ambient --save aswell although I don't know what exactly that does anyway.

I'm working off of this boilerplate https://github.com/mgechev/angular2-seed which in the wiki suggests that install modules is as easy as using npm install and then

 import * as jwt from 'angular2-jwt/angular2-jwt';

But I can't import my request module for some Reason.

This is my import line

import * as request from 'request';

Do I need to reference the module elsewhere somehow?

1
did you do npm install ?shershen
typings is for definition files. Did you try installing it using npm install request?Mike Cluck
yes I did npm install. I didn't mention that because I thought it was obviousgempir

1 Answers

2
votes

The request module isn't designed for browsers only for Node applications. You should use the browser-request instead.

That being said, installing the module with NPM isn't directly usable into your application:

  • For compilation you need to install a typing for compilation. A kind of contract of the API of your library. This way the TypeScript compilation will know which classes, methods and properties are present in the module.

  • For execution you need to reference your module when loading your application. For example with SystemJS you need something like that:

    System.config({
      map: {
        request: 'node_modules/browser-request/index.js'
      }
    });
    

    This way you will be able to import the library this way:

    import * as request from 'request';