Search code examples
cordovavimtypescriptionic-frameworkionic2

how to get rid of typescript error message "Cannot find module 'localforage'"


I am doing an ionic 2/cordova project and am using vim with typescript support.

i have the following line in my code:

import * as localforage from "localforage";

the code is running without problems but the typescript compiler gives the following error

/path/to/file.ts|2 col 30 error| Cannot find module 'localforage'.

any hints how to avoid that error?

UPDATE

import {Injectable} from "angular2/core";
import * as localforage from "localforage";

@Injectable()
export class DbService {

  constructor() {
    //this.run();
  }

  setKeyVal(k, v){
    console.log("db service setKeyVal");
    let ran = Math.floor(Math.random() * 1000) + 1;
    localforage.setItem(k, device.uuid).then(function () {
      return localforage.getItem(k);
    }).then(function (value) {
      console.log(value);
      // we got our value
      console.log(navigator.connection.type);
      console.log("setItem then");
    }).catch(function (err) {
      // we got an error
      console.log("setItem catch");
    });
  }
}

UPDATE 1 - typings.json

{
  "dependencies": {},
  "devDependencies": {},
  "ambientDependencies": {
    "cordova-ionic": "registry:dt/cordova-ionic#0.0.0+20160316155526",
    "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#4de74cb527395c13ba20b438c3a7a419ad931f1c",
    "localforage": "registry:dt/localforage#0.0.0+20160316155526"
  }
}

UPDATE 2 - tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "filesGlob": [
    "**/*.ts",
    "!node_modules/**/*"
  ],
  "exclude": [
    "node_modules",
    "typings/main",
    "typings/main.d.ts"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

Solution

  • First - install type definitions from: localforage

    Second - change your import to:

    import {localforage} from 'localforage';