How to import localforage with ionic2/angular2

Hey, how can i import and use localforage with ionic 2?

import {Injectable} from "angular2/core";
const localForage:LocalForage = require("localforage");

export class DbService {

  constructor() {;

    console.log("db service run");
    localForage.setItem('key', 'value').then(function () {
      return localforage.getItem('key');
    }).then(function (value) {
      // we got our value
      console.log("setItem then");
    }).catch(function (err) {
      // we got an error
      console.log("setItem catch");

gives an ReferenceError.

You have some inconsistent capitalization between “localForage” and “localforage”. Might that be the problem?

i solved it that way:

import * as localforage from "localforage";

Can you show exactly how?
Only using the import? Or the constant/require too?


Most recently I’ve been using this idiom:

export class Sink {
  private _lf:LocalForage;
  constructor() {
    this._lf = require<any>('localforage').default;

Freak took me way too long to figure out how to import correctly LocalForage. For a long time, my code was compiling but I was facing “undefined errors” while running it.

So here my solution to import and use LocalForage in a Ionic2 project:

  1. Install localforage

    sudo npm install localforage --save

  2. Add typings support

    typings install dt~localforage --save --global

  3. And finally how to use it in an injectable provider/service.

Note localforage, at least in my case, could not be imported like I mostly do with “import * as localforage from ‘localforage’;” because this will lead to the error “undefined” at runtime. To solve the problem it should be included thru require() defined first as Function.

import {Injectable} from '@angular/core';
declare var require:Function;

const localforage:LocalForage = require('localforage');

// My data model module containing my object class UserAccess
import {User} from  '../../model/user/user';

export class UserAccessStorageService {

    constructor() {
            name: 'MyApp'

    saveUser(user_data:User.UserAccess):Promise<User.UserAccess> {
        return localforage.setItem('myapp_user', user_data);

    retrieveUser():Promise<User.UserAccess> {
        return localforage.getItem('myapp_user');

    clear():Promise<void> {
        return localforage.clear();

Hope this will help.


I included localForage into an Ionic 2 project like this if anyone is still interested:

Hopefully it helps someone.