0

I recently installed unirest.

In their documentation they provided the following piece of code to test unirest:

var unirest = require('unirest');

    unirest.post('http://mockbin.com/request')
    .header('Accept', 'application/json')
    .send({ 'parameter': 23, 'foo': 'bar' })
    .end(function (response) {
      console.log(response.body);
    });

When I add this bit of code, I get the following error in my console:

Error: require is not defined

The entire UI of my site stops working as well.

I've used require in the past before and it's never given me this error. Is there something I'm forgetting to include?

EDIT: Forgot to mention. Yes, I am running this in a Node.js environment.

  • welcome to good documentation, where the examples work. just kidding, where did you put this file? maybe its outside of your node environment? (you're using node i'm guessing) – Abdul Ahmad Feb 05 '16 at 15:10
  • Are you running this in a Node.js environment? – SlashmanX Feb 05 '16 at 15:12
  • It's definitely within my node environment. I've installed plenty of packages while working on this app that are being used within this file. I don't think unirest itself is causing the issue. When I try to use the require function in general it still throws that error. Is there some package for require that I'm missing? – Shaun O Donovan Feb 05 '16 at 15:14
  • 1
    Just to be extra clear (sorry), this code is definitely server-side in Node.js? Seeing as you're saying that it prevents your UI working (which I assume is in Javascript), it seems like you've placed this code client-side, where `require` doesn't exist (normally), whereas it's a core feature of Node.js so should never be undefined – SlashmanX Feb 05 '16 at 15:17
  • @SlashmanX this code is client-side Javascript. I tried injecting 'unirest' as a dependency into the controller but the same issue occurred with UI. I get the feeling I'm going about this wrong. Is there another way to do this? – Shaun O Donovan Feb 05 '16 at 15:19
  • @ShaunODonovan - You can see this question for ways of using npm modules client-side: http://stackoverflow.com/questions/29326211/how-do-i-require-a-node-module-in-my-client-side-javascript (Browserify, webpack etc etc). I'm not sure if unirest supports browserify, but Superagent definitely does and it's pretty similar to unirest: https://github.com/visionmedia/superagent. Otherwise you can just use jQuery `post` `get` etc without needing browserify – SlashmanX Feb 05 '16 at 15:26
  • Maybe this will help : http://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined – Warren Reilly Feb 05 '16 at 15:35

0 Answers0