Help me with this final step of building a native .NET bindings module for Node using node-pre-gyp / pkg on Windows 10.
Tags: discuss, explainlikeimfive, node, native
Published: 2017-12-03 01:02:13

---
title: Help me with this final step of building a native .NET bindings module for Node using node-pre-gyp / pkg on Windows 10.
published: true
tags: discuss, explainlikeimfive, node, native
---

Node package _clr_ on NPM (https://www.npmjs.com/package/clr) binds .NET framework and allows loading DLL files during runtime. To NPM install _clr_ you need some additional build tools. Visual Studio is useful but not truly necessary since there is also a package called _windows-build-tools_.

CLR builds fine using various versions of Node but when using the more recent Node 8.x it now requires you to run Visual Studio 2017 to build.

### From readme.md
> This library is built and tested on following environment:
> 
> * Node.js v6.3.1
> * .NET Framework 4.5
> * Visual Studio 2015
> * Node.js native module build environment

We can use _pkg_ (<a href="https://www.npmjs.com/package/pkg">https://www.npmjs.com/package/pkg</a>) to package our Node projects to a single executable file. However when I add module _clr_ to its dependencies, the creation of the executable is successful but when calling _clr_ using require() I receive an error that the module did not self register. 

> AtsushiSuzuki commented on Mar 13
> If you need to distribute pre-compiled module, you can try node-pre-gyp.

_node-pre-gyp_ stands between *npm* and _node-gyp_ and offers a cross-platform method of binary deployment.
<a href="https://www.npmjs.com/package/node-pre-gyp">https://www.npmjs.com/package/node-pre-gyp</a>

I use the following package.json and then run *node-pre-gyp build*. The build is successful and it created a clr.node file.

```json
{
  "name": "clr",
  "version": "0.0.18",
  "engines": {
    "node": ">=0.12 <8"
  },
  "description": "Node.js binding for .NET Framework API",
  "repository": {
    "type": "git",
    "url": "https://github.com/AtsushiSuzuki/node-clr"
  },
  "main": "lib/clr.js",
  "scripts": {
    "test": "mocha"
  },
  "dependencies": {
    "bindings": "^1.2.1",
    "nan": "^2.4.0",
    "node-pre-gyp": "^0.6.38",
    "underscore": "^1.8.3"
  },
  "devDependencies": {
    "mocha": "*",
    "aws-sdk": "2.x"
  },
  "bundledDependencies":["node-pre-gyp"],
    "scripts": {
        "install": "node-pre-gyp install --fallback-to-build"
    },
    "binary": {
        "module_name": "clr",
        "module_path": "./lib/",
        "host": "https://clr.s3-us-west-1.amazonaws.com"
    },
  "keywords": [
    ".NET",
    "CLR",
    "Common Language Runtime",
    "API Bridge"
  ],
  "author": {
    "name": "Atsushi Suzuki",
    "email": "possibly.maybe.probably@gmail.com"
  },
  "license": "(ISC OR WTFPL)",
  "gypfile": true
}
```

I'm almost there. I can build _clr_ with _node-pre-gyp_ and it creates a *clr.node* file. However when I require it, it has no init() method like it is supposed to. In stead it looks like this:

```
{ 
	import: [Function],
	getAssemblies: [Function],
	getTypes: [Function],
	createConstructor: [Function],
	getMembers: [Function],
	invokeMethod: [Function],
	getField: [Function],
	setField: [Function],
	isCLRObject: [Function],
	getType: [Function],
	isCLRConstructor: [Function],
	typeOf: [Function],
	path: 'C:\\Users\\Administrator\\Desktop\\build\\Release\\clr.node' 
}
```

It doesn't matter if I require clr directly or use the _bindings_ method.

```js
var clr = require('clr') // or require('bindings')('clr.node'), no difference
```

> AtsushiSuzuki commented on Oct 1 on <a>GitHub</a>
> Great!

> node-clr has lib/clr.js as main script. lib/clr.js utilizes clr.node.
> If you do require(".") or require("lib/clr.js"), it will have documented APIs.

---

And this is where I'm currently stuck. The answer from Sushi is not helping. I feel stupid not figuring this out.