Using Stardog.js in the browser


(Andrea Westerinen) #1

I have downloaded, built and import the stardog.js code from GitHub (v1.3.0), but I cannot get my browser-based javascript to load all the dependencies. When the code hits a “require” statement, I get an “Uncaught ReferenceError: require is not defined” error.

Clearly, I have missed some step or did not correctly locate all the necessary files.

Thanks for any help …

Andrea


(Jason Rogers) #2

Hi Andrea,

Can you say a bit more about how you are building and trying to import stardog.js into your browser-based code? For example, are you using a module bundler (such as browserify or webpack), or are you trying to include stardog.js directly via a <script> tag? Describing your steps would help us to help you.

Assuming no module bundler, at the most basic level, you should be able to simply npm install stardog (no need to build it on your own), add a <script> tag to your HTML that references the distributed stardog.js code (e.g., <script src="./node_modules/stardog/dist/stardog.min.js"><script>), and then use stardog.js via the global stardogjs object. For example, if I had a basic index.html file in a directory named ‘app’, I would run npm install stardog in that directory, and then put the following in index.html:

<script src="./node_modules/stardog/dist/stardog.min.js"></script>
<script>
  const conn = new stardogjs.Connection({
    username: 'admin',
    password: 'admin',
    endpoint: 'http://localhost:5820',
  });

  stardogjs.query.execute(conn, 'myDatabaseName', 'select distinct ?s where { ?s ?p ?o }', 'application/sparql-results+json', {
    limit: 10,
    offset: 0,
  }).then(({ body }) => {
    console.log(body.results.bindings);
  });
</script>

I hope that helps.

Jason


(Andrea Westerinen) #3

Jason, Thank you for the help. I was going off the example at https://github.com/stardog-union/stardog.js which did not list the stardogjs class. I guessed at some values (like ‘Stardog’) but didn’t guess stardogjs. :slight_smile:

This might be in the docs and I missed it, or it might be missing from the docs (I thought that I read everything, even the legacy js docs).

I am working now.
Andrea


(Jason Rogers) #4

Glad to hear you’ve got things working now. You’re right that this is missing from the docs (sorry about that!). I’ve made a note to update them soon.

Thanks,
Jason