Search code examples
javascriptnode.jsnode-webkitpouchdbnw.js

NW.js - PouchDB - Unable to use PouchDB via require() in node webkit


I am new to pouchdb and nw.js and may be this question is a little bit too simple, (sorry for my ignorance).

I am trying to to use pouchdb in a nw.js project via require() but with no luck.

According to the documentation for the pouchdb setup , under Node.js section, I dit it exactly as it says with no success.

After that I installed leveldown component into the project, and I have followed the following instructions under https://github.com/nolanlawson/pouchdb-nw github project.

So, at this point, I have already done the following:

nw-gyp configure --target=0.12.3 / in the node_modules/leveldown directory

nw-gyp build

Then, according to pouchdb.com/guides/databases.html I have:

var PouchDB = require('pouchdb');
var db = new PouchDB('kittens');

but again with no luck. In addition, by running the following:

db.info().then(function (info) {console.log(info); });

getting no response.

Note: If just include this <script src="../node_modules/pouchdb/dist/pouchdb.min.js"></script> in the index.html file, everything works like a charm.

nw.js version: 0.12.3 / pouchdb version: 5.2.1

What am I missing?


Solution

  • Finally I found a working solution to my problem.

    The work-around is as follows:

    1) I updated pouchDB to 5.3.0

    npm update pouchdb --save

    2) Then navigate to node_modules/leveldown

    cd node_modules/leveldown

    3) configure gyp with nw.js target version

    sudo nw-gyp configure --target=0.12.3

    4) Build again the nw gyp

    nw-gyp build

    5) And then in my javascript module file

     var PouchDB = require('pouchdb');
        var arincPouchDB = new PouchDB('./db/arincAirports'); // new pouch db for node without adapter // means you get leveldb adapter in this case.
        var jsonData = require("../datasrc/output/data.json");
        arincPouchDB.bulkDocs(jsonData);
    
        arincPouchDB.info().then(function (info) {
            console.log(info);
        });
    

    6) And the console says...

    Objectadapter: "leveldb" 
    auto_compaction: false
    backend_adapter:"LevelDOWN"
    db_name: "./db/arincAirports"doc_count: 12
    update_seq: 12
    __proto__: Object...
    

    Note: If you try to use pouchDB from a script tag in your html file, and at the same time you have to use it in a javascript function, that will be exported via module exports e.g

    exports.pouchDBFunction = new pouchDBFunction();
    

    and to use this exported function in another javascript file, like this

    var json2PouchDB = require("./js/pouchDBFunction.js");
     json2PouchDB.pouchDBFunction;
    

    will not work and you will get the error pouchdb is not defined. That's why you need to have pouchDB via require() function.

    Hope this workaround helps...