Using Stardog.js in the browser

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

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

Jason, Thank you for the help. I was going off the example at GitHub - stardog-union/stardog.js: Stardog JavaScript Framework for node.js and the browser 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

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.