Question

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.

Was it helpful?

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'.

OTHER TIPS

have you tried using an absolute path to the source code of your module? (usually can be obtained from GitHub or a simple Google search)

for instance, if you want to load adm-zip module from an absolute path:

var AdmZip = require(pathToModuleSource + "/adm-zip");

add permissiosn to your package.json

    "permissions": [
        "usb",
    ],

https://developer.chrome.com/apps/declare_permissions#usb

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top