Since version 0.6 node js supports windows native builds which allows the windows dev to use visual studio to build both nodejs and the modules. In order to make a simple node module in visual studio you will have to link the obj file with the node.lib file that is generated when you compile the nodejs on visual studio. Here is a detailed walkthrough on how to successfully build a native node module in visual studio 2010.
Building node (to get the node.lib file)
First and foremost you have to download the latest code from node’s git (or a stable one if you prefer) and run the vcbuild.bat file that is in the source. This bat will create a visual studio solution and build node using the msbuild tool chain. Watch out that by default this will build a Debug version of node and will fail (at least at the moment) to sign node.exe because it tries to locate node.exe in the Release folder. If you want to do it right (which is not required) you can either edit the vcbuild.bat file or run “vcbuild Release”. Either way, you will end up with a Release or a Debug folder which contains both node.exe and node.lib files. You will need that node.lib to link against it later on.
Writing the Helloworld.cpp
A lot of blog posts have been written on how to right a simple native Hello World node module so I will mention only the basics here.
- Modules usually inherit from the ObjectWrap class and this is why we need the fairly obvious node.h include file.
- Each module has a constructor and a destructor where you can do your staff there but you must also specify an Init method to initialize the v8 bindings (expose the supported methods etc).
- Finally you must not forget to add the extern “C” declaration where you specify the init subroutine (which simply points to your modules init method) and call to NODE_MODULE where you specify the module name and the init method (which is written right above).
So here is the HelloWorld.cpp we will be compiling in the following sections:
Setting up the visual studio project
Start by creating an empty Visual C++ project.
Add the HelloWorld.cpp file you have downloaded from above (don’t expect me to believe that you wrote it) and add it in the Source Files virtual folder. You may optionally add the “node.h” file located in the node’s source directory under the folder src, in the header files to improve your development experience. The project structure should look like the following:
Open the project properties (right click properties on the project) and go to the Configuration Properties→General tab. There you may specify the output directory etc but you must make sure that you select a Dynamic Library (.dll) in the Configuration Type. You may also specify that the target extension is a .node file to avoid renaming the dll output to .node (nodejs requires modules to have a .node extension). My property page look like the following:
Move on to Configuration Properties→VC++ Directories tab. Here you will have to add the include directories for node.h, v8.h and uv.h files and the location of the node.lib in the library directories.
All files are located in the node source tree you have compiled earlier (from now on I will refer to that location as %NODE_ROOT%). In the “Include Directories” you must add the following:
In the “Library Directories” you must add either %NODE_ROOT%\Release\ or %NODE_ROOT%\Debug\ depending on the exact location of your node.lib file and whether you need the debug symbols that are exposed by node or not.
There is also an alternative to this step. You may specify the Include Directories in the Configuration Properties→C/C++→General tab under the “Additional Include Directories” field, illustrated bellow:
As for the Library Directories, you may specify the exact path of the node.lib in the Linker (as show on a later step).
Based on the reading I did on how to build a native module on nodejs, most of the tutorials mentioned that you should add the “BUILDING_NODE_EXTENSION 1” def into your modules. My personal experience says that this is not requires in node v0.6 but if you want, you may specify this def in the Configuration Properties→C/C++→Preprocessor tab as shown in the next picture:
The final step is to go to the Configuration Properties→Linker→Input tab where you need to specify the node.lib as an “Additional Dependency”. If you skipped the VC++ Directories setup mentioned above, you should specify the full path to the node.lib file, otherwise a simple node.lib is sufficient:
That’s all. You may now build the project and you should see the following output:
If you haven’t set the target extension to .node you will have to manually rename it from .dll to .node and you will be to load the module by posting the require(‘pathtomodule’) command
Watch out that in my HelloWorld.cpp I expose the Hello function as hello (note the small h) on purpose to demonstrate that it doesn't matter what’s the name of the method in you cpp file, but the name specified in the v8 engine. You may download a test.js file to test your newly created module here:
I hope that this article will give you a heads up on the native windows nodejs module building process! You may also check a simple bat script I have prepared to autocompile the node module.