8

I'm facing an error while trying to build kurento-client-js with Webpack 2 + babel.

WARNING in ./node_modules/kurento-client/lib/register.js
60:20-33 Critical dependency: the request of a dependency is an expression

On execution it results in

Uncaught Error: Cannot find module "."

I believe that the issue itself is сaused by require inside /lib/register.js

//kurento-clinet/lib/register.js
if (constructor == undefined)
    return register(require(name));

And the code that cause errors:

//kurento-clinet/lib/index.js
//this module requires kurento-client resulting in circular reference
register('kurento-client-core') 

The kurento bower package contains distributive built with the browserify.

I wonder if anyone tried to build kurento-client-js using webpack. Please share your experience.

EDIT:

Circular dependency error stack trace:

Uncaught TypeError: Cannot read property 'MediaObject' of undefined
at Object._typeof (KurentoClient.js:42)
at __webpack_require__ (bootstrap 0d7eac46304670c5f3b5:19)
at Object._typeof (index.js:44)
at __webpack_require__ (bootstrap 0d7eac46304670c5f3b5:19)
at Object.module.exports (HubPort.js:21)
at __webpack_require__ (bootstrap 0d7eac46304670c5f3b5:19)
at Object._typeof (index.js:32)
at ...
kreig
  • 990
  • 11
  • 18
  • Sounds like something worth filing a bug for. It should be there responsibility. It's absolutely possible to build a nice bundle that will build with Webpack and also work with Node, but it seems like they have done their own complex build process instead. – loganfsmyth Aug 12 '17 at 18:19
  • @loganfsmyth, absolutely agree. Since kurento team was acquired by Twilio, they stopped active project development. I thought about rewriting the whole client from the scratch, but decided against it after diving deeper into sources. Almost all files and packages were auto-generated by some IDL tool and they look like complete mess now. It will take more than a week to reassemble the client. Of course, it would be great, because a lot of legacy code will be eliminated. However, I'm looking for a simpler solution so far. – kreig Aug 12 '17 at 23:09

1 Answers1

0

First of all webpack complains about a dynamic dependency (which cannot be resolved when building the bundle). It's not a circular dependency.

I got it working like this:

1) in your app's main.js require manually all the modules which the register() function might need

require('kurento-client-core')
require('kurento-client-elements')
require('kurento-client-filters')

const kc = require('kurento-client-core/lib/index.js')

console.log(kc)

2) use this webpack plugin to completely ignore unresolved/dynamic require() calls

//in webpack.config.js
plugins:[

function() {
  this.parser.plugin('call require', function(expr) {
    if (expr.arguments.length !== 1) {
      return;
    }

    const param = this.evaluateExpression(expr.arguments[0]);
    if (!param.isString() && !param.isConditional()) {
      return true;
    }
  });
}
//... other plugins
]

Webpack2 will warn about old plugin format, but it does work

Credits go to: https://stackoverflow.com/a/42527120/646156

Tudor Ilisoi
  • 2,934
  • 23
  • 25
  • Circular references are in both `kurento-client` and `kurento-client-core` libs. `kurento-client-core` requires `HubPort`, which requires `kurento-client`, which requires `kurento-client-core`. I tried your code, webpack assembles the package but it just don't work in browser, fails on `var MediaObject = require('kurento-client-core').abstracts.MediaObject;` (in `kurento-client` module referenced by `kurento-client-core`). – kreig Aug 19 '17 at 18:17
  • I have console.logged the kurento-client. It did work – Tudor Ilisoi Aug 19 '17 at 19:18
  • The `kc` variable in your code sample is an instance of the `kurento-client-core` module, not the `kurento-client`. I'm using kurento-client v6.6.2. `require('kurento-client/lib/index.js')` throws an error. – kreig Aug 19 '17 at 19:40
  • To put it otherwise, I have a working bundle with the above setup – Tudor Ilisoi Aug 19 '17 at 20:03
  • I really appreciate that you found time to answer my question, but it just doesn't work in my case. Maybe you are using another version of `kurento-client` module. I've attached an error to the original question to demonstrate circular dependency issue. – kreig Aug 19 '17 at 20:14
  • I get it now. Let me try that and get back to you – Tudor Ilisoi Aug 19 '17 at 20:42