Cannot load wasm URL scheme file is not supported

3.1k Views Asked by At

I am trying to learn how to use sql.js from here. https://sql.js.org/#/

I am following there first html example and I keep coming across errors just trying to run this.

I installed sql.js using npm install sql.js

I took the dist folder from the sql.js install and put it into the test folder where the index.html is.

I used there example code and tried open it in a browser but I am faced with: sql-wasm.js:167 Fetch API cannot load file:///C:/dist/sql-wasm.wasm. URL scheme "file" is not supported.

Code:

<meta charset="utf8" />
<html>
  <script src='C:\\Users\\Rocko\\Documents\\scripts\\AAOA\\nodetest\\dist\\sql-wasm.js'></script>
  <script>
    config = {
      locateFile: filename => `/dist/${filename}`
    }
    // The `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser.
    // We must specify this locateFile function if we are loading a wasm file from anywhere other than the current html page's folder.
    initSqlJs(config).then(function(SQL){
      //Create the database
      const db = new SQL.Database();
      // Run a query without reading the results
      db.run("CREATE TABLE test (col1, col2);");
      // Insert two rows: (1,111) and (2,222)
      db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);

      // Prepare a statement
      const stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
      stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}

      // Bind new values
      stmt.bind({$start:1, $end:2});
      while(stmt.step()) { //
        const row = stmt.getAsObject();
        console.log('Here is a row: ' + JSON.stringify(row));
      }
    });
  </script>
  <body>
    Output is in Javascript console
  </body>
</html>

Pictures: enter image description here

enter image description here

I have been trying to have my test webapp read my sqlite file for about 2 weeks now and I have been trying to follow what people have suggested. This is the latest suggestion and so I am trying to learn this but I can't even get the basic example done.

Any ideas would be appreciated.

Thanks

2

There are 2 best solutions below

0
On

I had the same error following the same page in the sql.js documentation.

The error is avoided by using sql-asm.js instead of sql-wasm.js. As the document says further down the page: "sql-asm.js The older asm.js version of Sql.js. Slower and larger. Provided for compatibility reasons."

This is preferable for me than spinning up a web server or trying to "encode the wasm as base64" as some other threads suggest (which is way beyond me while just trying to create a simple stand-alone .html page linked to a sqlite file).

1
On

You are trying to load the WASM file from a local file, which does not work.

But you can easily just spin up a webserver (by using e.g. NodeJS) on your local machine, and then run your test code from there, so it accesses it using a proper URL instead of a local file on your PC.