Search code examples
dexie

Consuming Dexie as a module


I'm just getting started with Dexie. I encountered an error I do not understand attempting to follow the Short Version example of Consuming Dexie as a module. I have the following source files:

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Dexie Test</title>
  <style>
  </style>
</head>

<body>
  <script src="./mytest.js" type="module"></script>
</body>

</html>

mydb.js

import Dexie from './dexie.js';

const db = new Dexie('myDb');
db.version(1).stores({
  friends: 'name, age'
});

export default db;

mytest.js

import db from './mydb.js';

console.log(db.name);

When I run this from a local web server, I get the following error:

Uncaught SyntaxError: The requested module './dexie.js' does not provide an export named 'default'

Solution

  • To use the ES module version of Dexie you will need dexie.mjs instead of dexie.js from https://unpkg.com/dexie@^3.0.0-rc.1/dist/dexie.mjs. Or if you pick it from node_modules, make sure you've installed dexie@next or dexie@^3.0.0-rc.1.