TooTallNate / node-bindings

Helper module for loading your native module's `.node` file

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic require() calls produce a warning in webpack

danieldietrich opened this issue · comments

My webpack builds currently produce the following warning for webpack { target: 'node' } (but not for { target: 'web' }:

WARNING in ./node_modules/bindings/bindings.js
81:22-40 Critical dependency: the request of a dependency is an expression
 @ ./node_modules/bindings/bindings.js
 ...

This is the line that produces the warning:

b = opts.path ? require.resolve(n) : require(n)

Versions:

bindings: 1.3.0
npm: 5.5.1
node: 8.2.0
webpack: 3.10.0
babel-core: 6.26.0
babel-loader: 7.1.2
babel-plugin-transform-es3-member-expression-literals: 6.22.0
babel-plugin-transform-es3-property-literals: 6.22.0
babel-plugin-transform-object-rest-spread: 6.26.0
babel-preset-es2015: 6.24.1

This seems to be related to webpack/webpack-dev-server#212

Here is a suggestion on how to solve it.

Same warning messages for me with "target": "electron-renderer" and trying to load a native node module.

A solution that worked for me is to add the following to webpack.config.js:

externals: {
  bindings: 'require("bindings")'
}

And also to copy the .node file to thebuild/ directory, as explained in Cinan's Integrate Native Node.js Modules Into an Electron App (1/2) blog post.

It would be nice to be able to configure webpack to avoid such coping though.

I had to use something like:

const odbc = require( "electron" ).remote.require( "odbc" );

Otherwise, I got an error.

commented

Neither of these worked for me as there were some downstream issues, but the comment here resolved it for me jsdom/jsdom#2066 (comment)

If you want to avoid using externals, this works