I got this error when I try to run and code related to Dexie.
Unhandled Promise rejection: ReferenceError: require is not defined
at eval (http://localhost:3001/vendor/traceur/dist/commonjs/traceur.js:5:92)
Evaluating http://localhost:3001/vendor/traceur/dist/commonjs/traceur.js
Error loading http://localhost:3001/vendor/traceur/dist/commonjs/traceur.js
Error loading http://localhost:3001/vendor/dexie/dist/dexie.js as "dexie" from http://localhost:3001/app/pages/portal/portal.js ; Zone: <root> ; Task: Promise.then ; Value: Error: ReferenceError: require is not defined(…)
Just by declaring a Database, I already have the above errors.
// Declare Database
class FriendDatabase extends Dexie {
friends: Dexie.Table<IFriend,number>;
constructor() {
super("FriendDatabase");
this.version(1).stores({
friends: "++id,name,age"
});
}
}
The convention way of me adding Dexie is in systemjs.config.js(in my case I've used Typescipt)
/** Map relative paths to URLs. */
const map: any = {
'dexie': 'vendor/dexie/dist',
'traceur': 'vendor/traceur/dist/commonjs'
};
/** User packages configuration. */
const packages: any = {
'dexie': { main: 'dexie.js', defaultExtension: 'js'},
'traceur': { main: 'traceur.js', defaultExtension: 'js'}
};
Edited My src code can be found here
Edited
Just to be clear. I'm not saying it is dexie's library issue. It could be that I'm not implementing in the right way. Hence, looking for a guide to implement dexie correctly in angular2.
Edited
I found this link, but the solution of adding the below code doesn't work for me
System.register("dexie", [], true, function(require, exports, module) {
var global = System.global,
__define = global.define;
global.define = undefined;
(function (global, factory) {
....
(Work around)
Not what, I'm able to barely get it working is by add dexie via script tag
<script src="vendor/dexie/dist/dexie.min.js"></script>
and then I can work with dexie straightaway.
But the problem to this is that I cannot import dexie and all the typings for dexie is missing.
I try to manual copy dexie.d.ts and reference it, but typescript does not acknowledge, so I would have compilation errors.
Finally after some googling.
In order to configure Dexie with SystemJS is found in this github link.
Copy the summary here
Install dexie via npm
npm install dexie --save
Update systemjs.config.js as follows:
Add 'dexie': 'node_modules/dexie/dist/dexie.js' to map.
Add 'dexie': { format: 'amd' } to packages.
Also: Make sure your tsconfig file has "moduleResolution": "node".