Consuming Dexie as a module

Short Version

import Dexie from 'dexie';

const db = new Dexie('myDb');
    friends: `name, age`

export default db;

Save the above code to for example mydatabase.js and import it from another module:

import db from './mydatabase';

Long Version

Dexie is written in ES6 and distributed in both the UMD and ES formats. It can be consumed either as a plain script tag, required as a CJS, AMD or imported as an ES module.

Vanilla scripts are nice when testing out something. But a module-based approach is better in the long term and package manager helps you keep track of your dependencies. There are lots of combinations of package- and module systems to choose from. For web apps, npm + webpack works perfectly well so let’s start with that alternative.

NPM and webpack

With NPM you keep track of versions and dependencies for your app. It’s also a perfect platform to use when using commonjs modules and webpack.

Assuming you’ve already installed nodejs that bundles npm with it. Start by initing your new npm package. CD to a brand new dir and do:

mkdir hello-dexie
cd hello-dexie
npm init
# Just press enter on all questions to generate your package.json
npm install dexie --save
npm install webpack -g

Write your javascript file (index.js or whatever) that uses dexie:


var Dexie = require('dexie');
var db = new Dexie('hellodb');
    tasks: '++id,date,description,done'

// Don't be confused over Dexie.spawn() and yield here. It's not required for using Dexie,
// but it really simplifies the code. If you're a Promise Ninja, use vanilla promise
// style instead.
Dexie.spawn(function*() {
    var id = yield db.tasks.put({date:, description: 'Test Dexie', done: 0});
    console.log("Got id " + id);
    // Now lets add a bunch of tasks
    yield db.tasks.bulkPut([
        {date:, description: 'Test Dexie bulkPut()', done: 1},
        {date:, description: 'Finish testing Dexie bulkPut()', done: 1}
    // Ok, so let's query it
    var tasks = yield db.tasks.where('done').above(0).toArray();
    console.log("Completed tasks: " + JSON.stringify(tasks, 0, 2));

    // Ok, so let's complete the 'Test Dexie' task.
    yield db.tasks
        .startsWithIgnoreCase('test dexi')
        .modify({done: 1});

    console.log ("All tasks should be completed now.");
    console.log ("Now let's delete all old tasks:");

    // And let's remove all old tasks:
    yield db.tasks

    console.log ("Done.");

}).catch (err => {
    console.error ("Uh oh! " + err.stack);

This script uses Dexie.spawn() and yield. You need a modern browser to open it. Note that Dexie does not require using the yield keyword, but it simplifies your code a lot if you do. Read more about this on Simplify with yield.

Now, create a HTML page:


        <meta charset="utf-8">
        <script type="text/javascript" src="bundle.js" charset="utf-8"></script>

As you can see, the page just includes a file called bundle.js. That is the file that webpack will generate when doing the following command:

webpack ./index.js bundle.js

Now your done to open your web page in a browser. If you’re on the Edge browser, you cant just open the page from your file system because it would block indexedDB. You could use the nice module http-server to start a local web server and access it from there.

npm install -g http-server
http-server .

Now start a browser towards http://localhost:8080/ and press F12 to view the console log output.


NPM and rollup


import Dexie from 'dexie';

var db = new Dexie('mydb');
db.version(1).stores({foo: 'id'});{id: 1, bar: 'hello rollup'}).then(id => {
}).then (item => {
    alert ("Found: " +;
}).catch (err => {
    alert ("Error: " + (err.stack || err));


        <meta charset="utf-8">
        <script type="text/javascript" src="bundle.js" charset="utf-8"></script>


npm install dexie --save
npm install rollup -g
rollup main.js -o bundle.js

The es6 version is located on but rollup will read the jsnext:main attribute in package.json, so it’s enough to just import ‘dexie’.


Dexie can also be installed via bower.

bower install dexie --save

It will show up in bower_components. You’ll have to configure requirejs accordningly, see requirejs below.


requirejs doesn’t find modules with tha magic that goes with npm and webpack. So you have to update your require config accordningly:

    paths: {
        "dexie": "node_modules/dexie/dist/dexie" // or the bower location, or simply

// And to consume it:
requirejs(['dexie'], function (Dexie) {
    var db = new Dexie('dbname');


System.js is also not that magic as npm and webpack. You need to configure both its location and its module type. Here’s how to do that:

npm install dexie --save


    map: {
        'dexie': 'node_modules/dexie/dist/dexie.js'
    packages: {
        'dexie': { format: 'amd' } // or 'cjs'


With typescript and npm, it’s super-easy. Just make sure to:

  • Use npm to install dexie npm install dexie --save
  • Make sure tsconfig has { moduleResolution: 'node' }

In your code, import dexie and subclass it:

// Import Dexie
import Dexie from 'dexie';

// Subclass it
class MyDatabase extends Dexie {
    contacts: Dexie.Table<IContact, number>;

    constructor (databaseName) {
            contacts: '++id,first,last'

interface IContact {
    id?: number,
    first: string,
    last: string

// Instantiate it
var db = new MyDatabase('myDb');

// Open it => {
    console.error(`Open failed: ${err.stack}`);

That’s it! Typings are delivered with the package. DON’T:use tsd or typings to add dexie’s type definitions. They are bundled with the lib and pointed out via package.json’s typings property.

See also Dexie Typescript Tutorial

Next steps

Architectural Overview


Look at some samples


Migrating existing DB to Dexie


Back to Tutorial

Table of Contents