3

I have spent a day trying to figure this out. Given my js knowledge is dated I end up sorting out a moshpit of 'new' technologies where every doc and author expects you to know these things. It all creates one big brainfog. So I ask if anyone can explain how i enable require and Request in the code Im trying out.

To be specific, I have a working php API on my server using Hawk HTTP authentication. I am trying out the example javascript from the Github repo.

https://github.com/hueniverse/hawk

Client Code:

const Request = require('request');
const Hawk = require('hawk');


// Client credentials

const credentials = {
    id: 'dh37fgj492je',
    key: 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn',
    algorithm: 'sha256'
}

// Request options

const requestOptions = {
    uri: 'http://example.com:8000/resource/1?b=1&a=2',
    method: 'GET',
    headers: {}
};

// Generate Authorization request header

const header = Hawk.client.header('http://example.com:8000/resource/1?b=1&a=2', 'GET', { credentials: credentials, ext: 'some-app-data' });
requestOptions.headers.Authorization = header.field;

// Send authenticated request

Request(requestOptions, function (error, response, body) {

    // Authenticate the server's response

    const isValid = Hawk.client.authenticate(response, credentials, header.artifacts, { payload: body });

    // Output results

    console.log(`${response.statusCode}: ${body}` + (isValid ? ' (valid)' : ' (invalid)'));
});

Error

ReferenceError: require is not defined

I can relate to that this would cause an error when require is not part of plain javascript. What will i need to understand/learn/implement to make this work? Note that this is all on the client side.

wyldcard
  • 601
  • 2
  • 7
  • 13
  • 2
    You need a module bundler, like [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/). – evolutionxbox Apr 18 '17 at 09:48
  • Possible duplicate of [Javascript require() function giving ReferenceError: require is not defined](http://stackoverflow.com/questions/23603514/javascript-require-function-giving-referenceerror-require-is-not-defined) – evolutionxbox Apr 18 '17 at 09:49
  • 1
    require() is not part of your standard JavaScript. In context to your question and tags, require() is built into Node.js to load modules. – King Reload Apr 18 '17 at 09:52

2 Answers2

4

The module you are looking at is designed to run under NodeJS (which has native support for require) and not in a browser.

You might be able to get it to run in the browser, but it is likely that you cannot due to the same origin policy and should not because it would leak your credentials to anyone who visits your website.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thank you for your reply. So a code for the client that is supposed to be run on a server... Don't make sense to me :) – wyldcard Apr 18 '17 at 10:24
  • 1
    @wyldcard — Don't conflate "NodeJS" with "Server". NodeJS is simply a way to run JavaScript programs. You can write client software in it. (And a single piece of software can be both a client and a server: A proxy server is a common example, it lists for requests from HTTP clients but then makes HTTP requests to other HTTP servers to get the data to give to the client). – Quentin Apr 18 '17 at 11:06
  • I accepted you answer and I understand. I did try it but I abandoned Hawk and went for JWT instead. Thank you. – wyldcard Apr 22 '17 at 20:36
1

require is part of the node API and is not available in the browser. In order to use modules in the browser, you will need to run your code through a bundler such as Webpack or Browserify first.

Be aware that there are still some modules (such as fs) which cannot run in the browser due to the way they interact with the system (i.e. reading a file from disk). I believe request falls in that same category.

I recommend you use Browserify to start out, due to its simplicity.

Andrei Nemes
  • 3,062
  • 1
  • 16
  • 22