webpack-contrib / node-loader

node loader for native modules
MIT License
119 stars 43 forks source link

Webpack relative path incorrect #22

Closed centerorbit closed 4 years ago

centerorbit commented 4 years ago

This issue is from trying to use the fix from #12 by @evilebottnawi

Expected Behavior

The node module is loaded via a relative path of './70a2ef50a7ff1fb36ab9b66e37a5f600.node' (or whatever other hash webpack decides to call the .node module)

Actual Behavior

An uncaught exception is thrown on the loading of the module:

/index.js:95
  throw new Error('node-loader:\n' + error);
  ^

Error: node-loader:
Error: //70a2ef50a7ff1fb36ab9b66e37a5f600.node: cannot open shared object file: No such file or directory

Code

import addon from './build/Release/build-napi-with-cmake.node';
console.log(addon.hello()); // 'world'

Webpack config:

...
module: {
    rules: [
      {
        test: /\.node$/,
        use: 'node-loader',
      },
    ],
  },
npm run build:js

> build-napi-with-cmake@0.0.0 build:js /home/andy/projects/abv-scaffolding/libraries/node-module-example
> webpack

Hash: b23aaa09303c0f2744da
Version: webpack 4.43.0
Time: 93ms
Built at: 07/01/2020 2:36:36 PM
                                Asset      Size  Chunks             Chunk Names
70a2ef50a7ff1fb36ab9b66e37a5f600.node  16.3 KiB          [emitted]
                             index.js  5.09 KiB       0  [emitted]  main
Entrypoint main = index.js
[0] ./build/Release/build-napi-with-cmake.node 190 bytes {0} [built]
[1] ./hello.js 165 bytes {0} [built]
[2] (webpack)/buildin/module.js 497 bytes {0} [built]

How Do We Reproduce?

Here is a repo to repro: https://github.com/centerorbit/node-loader-path-bug

All built files are committed, so you can run: npm run test. To run webpack again, run npm run build:js.


Error: //70a2ef50a7ff1fb36ab9b66e37a5f600.node

The '//' in front of '70a2...node' is telling. It's a result of this line: https://github.com/webpack-contrib/node-loader/blob/afc43f80046402774037a8c0de5e513f2795ffa1/src/index.js#L32

If you replace __dirname + "/" + __webpack_public_path__ with ./ the resulting webpack build works. I'm not versed enough in this modules intentions (or webpack in general) to know if this is the way to fix the problem, or if it's more a 'hack' (I suspect the later).

alexander-akait commented 4 years ago

https://github.com/webpack-contrib/node-loader#getting-started

You should set:

node: {
  __dirname: false,
},

webpack@5 fix this issue out of box

centerorbit commented 4 years ago

Ahhhh, so that's what that config is for! Thank you! Tested and it works perfectly!