Search code examples
javascriptnode.jsusblibusbnode-webkit

How to load external node module in node-webkit application


I have a problem with loading node-modules in my node-webkit application.

For example module usb (https://npmjs.org/package/usb).

It was successfully installed from npm (npm install usb --save-dev) and works in simple Node.js, also it was successfully rebuilded by nw-gyp for using in node-webkit.

I don't include node_modules folder in my app.nw, so after building using grunt, I have a following structure of files:

  • app.exe
  • nw.pak
  • ffmpegsumo.dll
  • icudt.dll
  • libEGL.dll
  • libGLESv2.dll
  • [node_modules]
    • [usb]
      • binding.gyp
      • package.json
      • ...
      • [build]
        • [Release]
          • usb_bindings.node
          • usb_bindings.lib
          • usb_bindings.bdb
          • usb_bindings.exp
          • [obj]
            • ...

When I try to load this module in my appliacation, using following code:

var usb = require('usb');

I get an error

Error: Cannot find module 'usb'

Why node-webkit can't load this module?

p.s. All default modules like path, url, http works.


Solution

  • In my case the problem has two reasons:

    1. If I want to use node modules, which written on C++, I can't give for my app custom name (in my case 'app'), I should use default name 'nw'. https://github.com/rogerwang/node-webkit/wiki/Using-Node-modules#3rd-party-modules-with-cc-addons

    2. As I understood, it's not possible to load modules into appliacation from external folder 'node_modules' using function 'require'.