How to use external JS in a service

Hello, I am trying to use Dexie in a service. I installed Dexie via npm, imported it into ember-cli via:

//ember-cli-build.js
app.import('node_modules/dexie/dist/dexie.min.js');

created a service file:

//app/services/dexie.js
import Service from '@ember/service';
import Dexie from 'dexie';

    export default Service.extend({
        db: null,
        init() {
            this._super(...arguments);
            this.db = new Dexie('MyDatabase');
            // Declare tables, IDs and indexes
            this.db.version(1).stores({
                friends: '++id, name, age'
            });
        },
        addFriend() {
            this.db.friends.add({
                name: 'Camilla',
                age: 25,
            });
        }
    });

Chrome gives me this error:

Error: Could not find module dexie imported from ember-omnia-write/services/dexie

So my question is, how do I import this Library correctly or how can I refer to Dexie? Couldnt find any useful help on google.

Greetings :slight_smile:

EDIT 1: I kinda fixed it myself now by adding these arguments to ember-cli-build

app.import('node_modules/dexie/dist/dexie.min.js', {
  using: [
    { transformation: 'amd', as: 'Dexie' }
  ]
});
1 Like

Glad you got it figured out! You can also look at ember-auto-import for importing non-Ember js packages. It’s pretty neat and even allows lazy loading.

3 Likes