DEV Community

Jochem Stoel
Jochem Stoel

Posted on • Updated on

Help me with this final step of building a native .NET bindings module for Node using node-pre-gyp / pkg on Windows 10.

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 (https://www.npmjs.com/package/pkg) 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.
https://www.npmjs.com/package/node-pre-gyp

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

{
  "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
}
Enter fullscreen mode Exit fullscreen mode

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' 
}
Enter fullscreen mode Exit fullscreen mode

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

var clr = require('clr') // or require('bindings')('clr.node'), no difference
Enter fullscreen mode Exit fullscreen mode

AtsushiSuzuki commented on Oct 1 on GitHub
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.

Top comments (1)

Collapse
 
jochemstoel profile image
Jochem Stoel

Seriously guys, is there nobody on dev.to who can tell me how to build this simple Node native module?