node.jselectronpcapnwjs

Can't use library on NW.js - was compiled against a different Node.js version using


My problem is that I want to import this library on a desktop app based on NW.js (the template is here).

import Cap from 'cap-rbfork' //set on a vue.js view

When I start the app, it doesn't load the page and I get this error on the window console:

  VM245:11 Uncaught Error: The module '/home/user/app/node_modules/cap-rbfork/build/Release/cap.node'
    was compiled against a different Node.js version using
    NODE_MODULE_VERSION 83. This version of Node.js requires
    NODE_MODULE_VERSION 72. Please try re-compiling or re-installing
    the module (for instance, using `npm rebuild` or `npm install`). Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
    at Object.Module._extensions..node (:9080/internal/modules/cjs/loader.js:955)
    at Module.load (:9080/internal/modules/cjs/loader.js:756)
    at Function.Module._load (:9080/internal/modules/cjs/loader.js:669)
    at Module.require (:9080/internal/modules/cjs/loader.js:796)
    at require (:9080/internal/modules/cjs/helpers.js:68)
    at Object.<anonymous> (:9080/home/user/app/node_modules/cap-rbfork/lib/Cap.js:3)
    at Module._compile (:9080/internal/modules/cjs/loader.js:893)
    at Object.Module._extensions..js (:9080/internal/modules/cjs/loader.js:910)
    at Module.load (:9080/internal/modules/cjs/loader.js:756)
    at Function.Module._load (:9080/internal/modules/cjs/loader.js:669)

@Jaredcheeda

I followed your instructions, and I created a new NWjs app with the template you suggested.

  mounted(){
    var Cap = this.nw.require('cap-rbfork')
    console.log(Cap) //it lists properties
    console.log(Cap.Cap.deviceList()) //or Cap.deviceList(), but the app crashes anyway, without reporting the reason, except SIGSEGV
  }

If I visit the page from the browser, I get this error:

vue.runtime.esm.js?2b0e:1897 Error: Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
    at eval (cap.node:1)
    at Object../node_modules/cap-rbfork/build/Release/cap.node (chunk-vendors.js:22)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at eval (Cap.js?c745:3)
    at Object../node_modules/cap-rbfork/lib/Cap.js (chunk-vendors.js:33)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at VueComponent.mounted (App.vue?234e:37)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1863)

I don't know this means that NW needs to be downgraded, so it can match the correct node-gyp version. I created an Electron project, and following this answer to a similar problem, it worked. Is there a NW equivalent to electron-rebuild?


Solution

    1. Completely uninstall your globally installed version of Node
    2. Install a Node Version Manager:
    3. Check the version of Node that is built in to NW.js
    4. Use your nvm tool of choice to install and switch to the matching Node version
    5. Delete the package-lock.json and node_modules folder
    6. Do npm install this will download/build files specific to your global Node versions, which should match the version built in to NW.js

    With all that said, Vue-CLI's Webpack/Babel set up is designed to import/bundle/tree-shake BROWSER code, not NODE code. So doing import x from 'y' won't work if it involves native code, node-bindings, executables, or Node specific code.

    NW.js uses require instead of import to handle importing Node modules.

    You may also need to change your NW.js version if you are having issues with Node-GYP/NW-GYP: