Require('spark') throws a ReferenceError

I’m not having much luck with SparkJS and have probably missed something really simple. I’m following the instructions at I go to and click the big green install button, click “Run” and the installer installs NodeJS. Then I open a command line and type “npm install spark”. It displays this:

spark@0.2.1 node_modules\spark
├── when@3.4.6
└── request@2.44.0 (caseless@0.6.0, aws-sign2@0.5.0, forever-agent@0.5.2, tunnel-agent@0.4.0, json-stringify-safe@5.0.0, oauth-sign@0.4.0, stringstream@0.0.4, qs@1.2.2, mime-types@1.0.2, node-uuid@1.4.1, tough-cookie@0.12.1, hawk@1.1.1, http-signature@0.10.0, bl@0.9.3, form-data@0.1.4)

I assume this is correct.
Then I create this HTML file:

<!DOCTYPE html>
<!-- Source file: index.html -->
        <p>Testing SparkJS</p>
        <script type="text/javascript" src="//">
        <script src="js\index.js"></script>

and this JavaScript file:

 * Source file: index.js
 * Javascript code included in index.html.

var spark = require('spark');

I then open index.html in a browser (in this case I used Chrome) and I get this message in the browser console window:

Clearly it has no idea about the require function. Now I think the require function is part of NodeJS but there is nothing in the HTML that references NodeJS and I cannot see anything that does this in the example documents. So, what is it I have to do to gain access to the require function? Any help appreciated.

I am doing all this on a Windows 7 computer.

Heya @RichardE!

You’re mixing frontend and backend code there - you won’t need the node.js setup if you only want to use the SparkJS in a client-side webapp.

The <script type="text/javascript" src=""></script> will load the SparkJS via a CDN, defining spark in global scope to be used.

As example: in your index.js just call spark.login(..., function(data) { /* this is called when done */ });without any previous require calls.

Enjoy! :sunflower:

Thanks @rastapasta, that works fine.
It was the docs on the JavaScript SDK that threw me. The example shows a call to request immediately followed by usage of the spark variable it returns. So when would you need to use require? Is the example intended to be run through node.js in a headless way and not on a webpage?

The docs can indeed be a bit confusing as it handles both use cases (server-side/headless and client-side) at the same time.

Require is node’s way to load modules into the current files scope - nearly same as what <script src=.../> does in your browser.

So you’ll need to require the sparkjs in your node server-side scripts if you want to access the Spark API in your server scripts (like implementing abstracted accessors to the core or advanced business logic that you don’t want to run on the client)

Hope that helped! :sunflower: