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

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?

Did you try the demo? https://github.com/nolanlawson/pouchdb-nw-hello-world There are quite a few steps to install PouchDB correctly in NW.JS.
Edit: oh wait, yes, you did see the demo already. Maybe the issue is that LevelDOWN is incompatible with your version of NW? In my demo I was using an older version of NW than you.
Yet another option is to use the websql adapter inside of Node, which should give you similar performance to LevelDB while possibly being easier to compile than LevelDOWN. If all else fails, I'd recommend filing an issue on the LevelDOWN repo; they have tons of issues related to building for X version of Node on Y architecture on Z operating system, so your combination of XYZ might be a unique one.

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...

Related

Problems using aws-sdk in the browser with browserify

I am working on browser based application that makes use of aws-sdk. I am using browserify for my app code but have not figured out how to roll aws into it. I have tried a couple of different approaches:
//MyApp.js - Take 1 using downloaded minified version
var AWS = require ('./aws-sdk.min.js');
...
AWS.config.region='us-east-2';
...
results in Cannot set property 'region' of undefined
My guess is that this doesn't work because browserify does not resolve the minified code.
//MyApp.js - Take 2 using downloaded development version
var AWS = require ('./aws-sdk.js');
This does not compile. Browserify reports Error: Cannot find module '../lib/core'.
Is there a trick to this that I am missing?
When I used AWS in the browser I set my region depending on the service I need, for example:
new AWS.EC2({apiVersion: '2016-11-15', credentials, region})
So this made me wonder, maybe the version you downloaded is encapsulated and not exposing anything for browserify.
First I tested the version in the browser as follows:
console.log(AWS)
<script src="https://cdnjs.cloudflare.com/ajax/libs/aws-sdk/2.184.0/aws-sdk.min.js"></script>
Everything looked good so then I went ahead and tested on browserify.
Turns out you are reassigning the AWS global variable when you do:
var AWS = require ('./aws-sdk.min.js');
But you are already bundling it, so you are good, what you need to do is the following:
require ('./aws-sdk.min.js');
// And then use it happily
AWS.config.region='us-east-2';
Without reassigning the AWS global variable

Better coffeescript workaround to load this node.js module

I would like to use this node.js module https://github.com/mlin/node-assert-type
Based on documentation, to declare the module;
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
In actual practice, this does not work. Some coffeescript error appears.
I have to make the following workaround;
var cs = require("coffee-script/register");//this line needed to require("assert-type")
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
To use this module, I am forced to install coffeescript with npm install -g coffeescript.
Is there some way to omit the line var cs = require("coffee-script/register");? After all, the module itself is using coffeescript locally. Am I doing it the right way?
Is it a normal practice to add a line to load coffeescript for node.js modules which use coffee-script?
It is not normal practice. I mean, it would be inevitable that coffeescript gets installed since it is a dependency but the user of the module should not worry about it.
I just took a quick look at the source code of the assert-type and this is what I found:
the project is 3 years old. That's a lot!
the package.json is listing coffeescript as a dependency BUT it is using latest instead of locking a version of coffeescript which is a terrible practice.
My guess is that what it changed was coffeescript module, that instead of needing require('coffeescript') you now need require('coffeescript/register'). (Take a look at the index.js in the repo)
Based on that I'd say it is fine that you write that line. A better option would be to make the changes in the node-assert-type repo and submit a PR with the fixes for #2 and #3.
Hope that helps.

Ember dependencies "Uncaught TypeError: $(...).fitText is not a function"

I'm attempting to use a jQuery plugin (in this case 'fitText') in an Ember project.
In the ember-cli-build.js (that replaced the Brocofile recently...) - I import the dependency (which I have already installed with bower).
/* global require, module */
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
// Add options here
});
app.import('bower_components/fittext/fittext.js');
return app.toTree();
};
In my console, I can type in 'fitText' and I DO get the object back. So, it's global now, and I should be able to use it anywhere. BUT "Uncaught TypeError: $(...).fitText is not a function"
#Tom Netzband pointed out that the bower version of fitText was the one that #adacio maintains and is vanilla JS. I've since switched it out for a app.import('vendor/jquery.fittext.js'); (the JS is really only 20 lines or so)
Previously I needed to import it in the controller or route where I want to use it - but the docs don't reference that anymore. In the docs with moment.js as the example, it seems to become a global.
I've put it in the ready hook in app.js - next to similar jQuery things that work fine... but no luck. In this case I only want to affect a few words on the index.hbs / route
UPDATE
This has really just become another question now. I thought it was still the same problem but it's not. So awarding answer and asking a new one. Thanks.
It looks like bower install fittext doesn't install the jQuery plugin version of fitText, it only installs the plain js version of it.
If you look at your bower_components/fittext/fittext.js file, you'll see jQuery isn't passed in or used, and the example from the example.html file shows usage as being:
fitText(document.getElementById('fittext'), 1.2)
So if you want the jQuery version you might have to download it straight from github and throw it in your vendor folder and include it outside of bower.
UPDATE
Looks like the version registered with bower is a forked version from the jQuery version where jQuery has been removed: https://github.com/adactio/FitText.js
There is in fact a Bower version of the jQuery version of the library.
bower install FitText.js --save
I think Dave Rupert has a jQuery version in his crates as well.
$ bower install jquery-fittext --save

Socket.configure Undefined is not a function error

Note: I am using Mac OS 10.10 Yosemite
Important Note: None of the other questions and answers have worked for me.
I am following a tutorial which will have it so that I could have a multiplayer game. There is a file, which I have to download, which has a game.js file that I need to add this code into:
Note: I correctly downloaded socket.io in the correct directory.
var util = require("util"),
io = require("socket.io").listen(80);
var socket,
players;
function init() {
players = [];
socket = io.listen(8000);
socket.configure(function() {
socket.set("transports", ["websocket"]);
socket.set("log level", 2);
});
};
init();
But when I run node game.js, I get an error that looks like this:
Note: The Robhawks-mozzilla-festival-92336f2 folder is the folder that has all of the files in it
Why is the socket.configure messing up? Also, how can I fix it?
.configure() was removed when socket.io went to version 1.0. I'm guessing that the tutorial you're following is using an older version (0.9), but you have installed 1.0.
It would be best to move your code base to 1.0, as it's the latest. Configuration should be done as part of the server initialization:
var socket = require('socket.io')({
transports : [ 'websocket' ],
...
});
More info here.
However, since you're following a tutorial it may be easier to first get things up and running using the older version of socket.io, and once you have familiarized yourself with it, move to 1.0. In that case, install the older version:
$ npm install socket.io#0.9
Looging socket.io v1.0 log-level option is removed. Thus for logging one has to start program using debug module.
install debug: npm install debug -S
then run the program: DEBUG=* node entry_file.js

How to use oauthorizer in Firefox addon

I am trying to use oauth2 in Firefox, and I found this module from Google:
https://github.com/mozilla/oauthorizer
However, I am not sure how to use it.
I tried the following according to README of the project,
Components.utils.import("resource://oauthorizer/modules/oauthconsumer.js");
Yet it reports error of following:
"`Components` is not available in this context.Functionality provided by Components may be available in an SDKmodule: https://developer.mozilla.org/en-US/Add-ons/SDK However, if you still need to import Components, you may use the`chrome` module's properties for shortcuts to Component properties:Shortcuts: Cc = Components.classes Ci = Components.interfaces Cu = Components.utils CC = Components.Constructor Example: let { Cc, Ci } = require('chrome');"
Then, I tried this:
let {Cu} = require("chrome");
Cu.import("resource://oauthorizer/modules/oauthconsumer.js");
But I got this error then:
"Component returned failure code: 0x80040111 (NS_ERROR_NOT_AVAILABLE)
[nsIXPCComponents_Utils.import]"
May be I need to set up something in the package.json? Or there are some special mechanisms to import the external modules?
Any help is much appreciated!
Finally, this is what I did: (Please note that I used jpm for the addon development)
Install oauthorizer using npm. Inside the root folder of addon, run the following:
npm install oauthorizer --save
In the addon script (index.js), add the following:
var OAuthConsumer = require("oauthorizer/lib/oauthconsumer.js").OAuthConsumer;
Modify the oauthconsumer.js a little bit to update the path:
e.g. replace require("sha1") to require("./sha1")
I am pretty sure it's not the best solution, but at least it works.

Categories

Resources