Friday, January 4, 2013

Getting started with Couchbase and node.js on Windows

In a previous post I have explained how to use Couchbase and Node.js on OS X. Since it is quite different on Windows here another article about it.

Install Couchbase Server 2.0

If you have not installed Couchbase Server already, do the following :
  1. Download Couchbase Server from here
  2. Run the installer
  3. Configure the database at http://localhost:8091 (if you have issue take a look to this article)
These steps are documented in the Couchbase Server Manual.

Install Node 

Install latest version of node

It is quite easy to install Node.js using the Windows installer provided at http://nodejs.org.

Once you have installed node, you can test is using the command line interface:

c:\Users\tgrall>node
> console.log(process.version);
v0.8.16

Node is installed. So far so good !

Install Couchnode

Couchnode, the Couchbase Client Library for Node.js, is a native module. The tool used to install native modules is node-gyp.  So to install couchnode you need to install :

  • node-gyp
  • python
  • Visual Studio to have access to a C/C++ compiler

Install node-gyp

The node-gyp module is easy to install and you can install it using npm using the following command:

npm install -g node-gyp

The -g parameter indicates that this module will be installed globally and added to your %PATH%.

Install Python

GYP uses Python to generate the project, so you need to install it on your environment. I have installed Python 2.7.3 using the Windows installer.

Install Visual Studio

Finally you need a C/C++ compiler, the best way to get it is to install Visual Studio. As you probably know I am not a Windows expert and I do not know a lot about Microsoft development tools. I have downloaded Visual Studio Express the free development tools from here; it was sufficient.

Install Libcouchbase for Windows

Couchnode uses libcouchbase the C client library, so before running the npm install for Couchbase, you need to install libcouchbase itself.

You can download it from Couchbase site. The Windows versions are located in the left menu of the page. Download the zip file, that matches your environment. I have downloaded the "Windows, 64-bit MSVC 10".

Node-gyp will look for all the dependencies (DLL, library headers) into c:\couchbase directory, so you need to unzip the file in this folder. This location comes from the binding.gyp file of the couchnode project.

Install and Test Couchnode itself!

Let's check what we have done so far; we have installed:
  • Node
  • node-gyp
  • Python
  • Visual Studio
  • Libcouchbase
We are now ready to install and use couchnode itself. For this we can create a new node project.

mkdir my-app
cd my-app
npm install couchbase

The install command will:
  • Create a node_modules folder and put couchbase client library in it
  • When installing/building couchnode on Windows I had the following warning :
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.CppBuild.targets(1138,5): warning MSB8012: TargetExt(.dll) does not match the Linker's Output
File property value (.node). This may cause your project to build incorrectly.
To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).
[C:\Users\tgrall\node\node_modules\couchbase\build\couchbase_impl.vcxproj]

This is only a warning and as far as I know, it is not a blocker. At the end of the log you should see:

couchbase@0.0.10 node_modules\couchbase
├── bindings@1.0.0
└── request@2.11.4


You have successfully installed couchnode.

Let's now write a small test. Create a test.js file with the following content:
var  driver = require('couchbase');

driver.connect({
  "username": "",
  "password": "",
  "hostname": "localhost:8091",
  "bucket": "default"}, 
  function(err, cb) {
    if (err) {
      throw (err)
    }
 
 var key = 'foo';
 cb.set(key, '{"server" : "couchbase", "version" : 2 }' , function (err, meta) {
  if (err) { console.log(err); }
        cb.get(key, function(err, doc) {
   if (err){ console.log(err);}
   console.log(doc);
        });  
 });
});


Run this with the command:

node test.js


You should see the following text in your console :
{ server: 'couchbase', version: 2 }

Conclusion

In this article you have learned how to:
  • Install Couchbase
  • Install Node
  • Install and configure node-gyp
  • Install and use Couchbase and Node
all this on Windows 7.

6 comments:

Anonymous said...

That is not working. Its giving an error: could not load the Visual C++ component VCBuild.exe

Moreover, how do we install libcouchbase on windows? We just have to put sub folders in libcouchbase, namely: bin,doc,include and lib in C:/couchbase or anything else?

Anonymous said...

That is not working. Its giving an error: could not load the Visual C++ component VCBuild.exe

Moreover, how do we install libcouchbase on windows? We just have to put sub folders in libcouchbase, namely: bin,doc,include and lib in C:/couchbase or anything else?

Anonymous said...

for windows 8 and if installing Visual Studio Express:

-- choose Visual Studio Express 2012 for Windows Desktop

-- before running "npm install couchbase", open the "Developer Command Prompt for VS2012" change directory to where you're installing couchbase and then run the command "npm install couchbase" inside this command prompt.

To find it go to Programs:
Microsoft Visual Studio 2012 Express --> Visual Studio Tools --> Developer Command Prompt for VS2012

or

Launch this: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat

Tug Grall said...

To build you need to have the libcouchbase in c:\couchbase.

I put also the DLL for runtime into the Windows/System folder.

Unknown said...

Can I copy folder node_modules/couchbase to others machine without rebuild couchbase on that machine? I've tried to do like that. Some machine can run but some machine got error message: "Error: The specified procedure could not be found". Thanks in advance.

hagi said...

Hi,

thanks for the helpful instructions.
I installed this successfully.However,i have some issues running your Couchbase Twitter Dashboard app.

I can list the users,but when i click on a user i get the error below:
https://www.dropbox.com/s/bmbk6kgxu82emas/Screenshot%202013-11-26%2000.09.43.png

Can you please help out?

thanks