0

We are doing a web using HTML and JavaScript. We have some problems when trying to do the authorization process to connect to the Spotify API.

var express = require('express'); // Express web server framework
var request = require('request'); // "Request" library
var querystring = require('querystring');
var cookieParser = require('cookie-parser')

This code we found on the internet uses require() to get some data but Chrome says:

Reference error: require is not defined.

We found that the JavaScript node may be the problem so we installed npm and Browserify but is not working.

We also used this in our HTML:

<script type="text/javascript" src="node.js"></script>
<script type="text/javascript" src="js/main.js"></script>

HELP??

  • That code belongs on the server side - it will not work outside of the Node environment. I highly recommend you look into Node if you want to use this API; here is [one place of many](https://www.airpair.com/javascript/node-js-tutorial) to get started. – Nick Zuber Jan 10 '16 at 12:33

2 Answers2

0

require() is not an existing method for client-side JS.

Use a <script> tag then call the method after it.

This was already answered here: Client on node: Uncaught ReferenceError: require is not defined\

Node.JS is a serverside application and you are attempting to run this clientside.

Please try using and referring to this http://requirejs.org/

Community
  • 1
  • 1
uccblack
  • 107
  • 2
  • 9
  • I have followed the steps in the other questions and now I get this error: Error: Module name "express" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded – m.herrero11 Jan 10 '16 at 18:26
0

What you have is the code that suppose to run on the server side. Node, Express, are server side components, and you can't run them on the clients side (inside of the browser). Server side is using 'require' to manage and load modules, that is why you have require there.

I think what should you do is to send your request to the server and server should process authentication code that you have there and send response back with the status if user authenticated or not.

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179