Mark Pazon
Mark Pazon

Reputation: 6205

Unable to use npm request package to meteor app

I executed meteor npm install --save request in the command line

I imported the request library in my code import {request} from 'request'

And tried to use it with

request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log(body) // Show the HTML for the Google homepage.
    }
})

However I keep on getting the following error:

undefined is not a function

How do I use the npm request package with my meteor app?

Upvotes: 1

Views: 235

Answers (1)

David Weldon
David Weldon

Reputation: 64312

The default export from the request package is the object you are looking for. Change your import statement to the following:

import request from 'request';

It may be that you need the low-level functionality from request, however your example could also be accomplished with meteor's HTTP pacakge (which is itself a wrapper around request).

Here's an example:

import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';

Meteor.startup(() => {
  const resp = HTTP.get('http://www.google.com');
  console.log(resp.content);
});

Note you'll need to run meteor add http for that to work.

Upvotes: 2

Related Questions