Thursday, September 13, 2012

couchnode meets npm

Earlier today I played around with npm to see if I could make your life easier when you're going to use couchnode to access your Couchbase cluster from your node app.

It turns out that it was pretty easy to create the package and upload it, but getting it to work the way I want to wasn't just as easy. Couchnode is built on top of libcouchbase, which means that you must have libcouchbase installed before you can try to install couchnode. I tried to figure out the recommended way to check for such dependencies in an npm module, but unfortunately I don't know yet (please drop me a note if you know how I should fix it!!!).

With that in mind, let's go ahead and install libcouchbase. The easiest would of course be to install this globally in /usr, but for the example of it I'll going to install it in my home directory under opt:

wget -O libcouchbase.tar.gz http://packages.couchbase.com/clients/c/libcouchbase-2.0.0beta.tar.gz
tar xfz libcouchbase.tar.gz
cd libcouchbase-2.0.0beta
./configure --prefix=$HOME/opt
make install

Since I didn't install libcouchbase into the "default location" for  libraries on the system I need to export some variables so that npm finds libcouchbase during npm install:

CPPFLAGS="-I$HOME/opt/include"
LDFLAGS="-L$HOME/opt/lib -Wl,-rpath,$HOME/opt/lib"
export CPPFLAGS LDFLAGS

You should now be able to install the couchnode driver with:

npm install couchbase

And you're good to go to use the couchnode driver from your web application :)




8 comments:

  1. You can also install binary packages:

    RedHat/CentOS

    $ sudo wget -O/etc/yum.repos.d/couchbase.repo http://packages.couchbase.com/preview/rpm/couchbase-centos$VER-$ARCH.repo
    $ sudo yum check-update
    $ sudo yum install libcouchbase-devel

    Where VER is one of 55 or 62, ARCH i386 or x86_64

    Ubuntu/Debian

    $ sudo wget -O/etc/apt/source.list.d/couchbase.list http://packages.couchbase.com/preview/ubuntu/couchbase-ubuntu$VER.list
    $ wget -O- http://packages.couchbase.com/ubuntu/couchbase.key | sudo apt-key add -
    $ sudo apt-get update
    $ sudo apt-get install libcouchbase-dev

    Where VER is either 1004 or 1110. Pick up first if your system doesn't have libevent2.

    And also build from sources via Hombrew on MacOS

    $ brew install https://github.com/couchbase/homebrew/raw/preview/Library/Formula/libcouchbase.rb

    ReplyDelete
  2. I was able to make progress on requiring Couchnode, but when I run it I get this error:

    > cb.Couchbase("localhost:8091")
    Assertion failed: (handle->InternalFieldCount() > 0), function Wrap, file ../src/node_object_wrap.h, line 71.
    Abort trap: 6

    ReplyDelete
  3. As far as making libcouchbase a dependency, I think your best bet may be to include the source, then use node-gyp [1] to compile that. node-gyp has replaced node-waf for native add ons [2]. More info on node-gyp [3]. I've been playing around with native addons, but I'm still a noob. Still, this is the place to start.

    [1] https://github.com/TooTallNate/node-gyp
    [2] http://nodejs.org/api/addons.html
    [3] http://code.google.com/p/gyp/wiki/GypUserDocumentation

    ReplyDelete
    Replies
    1. Yeah, check out how libncurses is bound:
      https://github.com/mscdex/node-ncurses
      Looks like there are recursive .gyp files.

      Delete
    2. Part of the problem is that we currently don't use gyp to build libcouchbase..

      Delete
  4. Hi,

    I am getting the following error while installing couchnode.. I also executed the steps in the blog.. but still getting the same issue.. Any help is appreciated. Thanks.

    cts@cts-OptiPlex-3010:~$ npm install couchbase
    npm http GET https://registry.npmjs.org/couchbase
    npm http 304 https://registry.npmjs.org/couchbase
    npm http GET https://registry.npmjs.org/bindings
    npm http 304 https://registry.npmjs.org/bindings

    > couchbase@0.0.12 install /home/cts/node_modules/couchbase
    > node-gyp rebuild

    make: Entering directory `/home/cts/node_modules/couchbase/build'
    CXX(target) Release/obj.target/couchbase_impl/src/couchbase_impl.o
    In file included from ../src/couchbase_impl.cc:21:0:
    ../src/couchbase_impl.h:39:18: fatal error: node.h: No such file or directory
    compilation terminated.
    make: *** [Release/obj.target/couchbase_impl/src/couchbase_impl.o] Error 1
    make: Leaving directory `/home/cts/node_modules/couchbase/build'
    gyp ERR! build error
    gyp ERR! stack Error: `make` failed with exit code: 2
    gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:255:23)
    gyp ERR! stack at ChildProcess.EventEmitter.emit (events.js:99:17)
    gyp ERR! stack at Process._handle.onexit (child_process.js:678:10)
    gyp ERR! System Linux 3.2.0-41-generic-pae
    gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
    gyp ERR! cwd /home/cts/node_modules/couchbase
    gyp ERR! node -v v0.8.18
    gyp ERR! node-gyp -v v0.8.2
    gyp ERR! not ok
    npm ERR! couchbase@0.0.12 install: `node-gyp rebuild`
    npm ERR! `sh "-c" "node-gyp rebuild"` failed with 1
    npm ERR!
    npm ERR! Failed at the couchbase@0.0.12 install script.
    npm ERR! This is most likely a problem with the couchbase package,
    npm ERR! not with npm itself.
    npm ERR! Tell the author that this fails on your system:
    npm ERR! node-gyp rebuild
    npm ERR! You can get their info via:
    npm ERR! npm owner ls couchbase
    npm ERR! There is likely additional logging output above.

    npm ERR! System Linux 3.2.0-41-generic-pae
    npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "couchbase"
    npm ERR! cwd /home/cts
    npm ERR! node -v v0.8.18
    npm ERR! npm -v 1.2.2
    npm ERR! code ELIFECYCLE
    npm ERR!
    npm ERR! Additional logging details can be found in:
    npm ERR! /home/cts/npm-debug.log
    npm ERR! not ok code 0
    cts@cts-OptiPlex-3010:~$

    ReplyDelete
    Replies
    1. Forgot to add..

      I am using Ubuntu 12.04 OS; I have already installed Couchbase community version (couchbase-server-community_x86_2.0.1.deb)

      Delete