webpack-contrib / node-loader

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

Error loading native node module after upgrading from 0.6.0 to 1.0.2 #37

Open pdehne opened 3 years ago

pdehne commented 3 years ago

Expected Behavior

I am using a custom native node module in my Electron application. When using node-loader 0.6.0 it is loading fine. When using node-loader 1.0.2 with the very same node module (no recompilation) I get an error.

Actual Behavior

The error thrown is:

soniccore.node:5 Uncaught Error: node-loader: Error: Invalid package C:\d\sw\trunk\SonicCare\node_modules\electron\dist\resources\electron.asar at Object../build/Release/soniccore.node (soniccore.node:5)

Code


// webpack.config.js

target: "electron-renderer",

{
    test: /\.node$/,
    loader: 'node-loader',
    options: {
        name: '[path][name].[ext]'
    }
}

// The complete webpack.config.js:
// https://gist.github.com/pdehne/54a6a6f91435e332c70d5e00abeed20f

import soniccore from '../build/Release/soniccore.node';
alexander-akait commented 3 years ago

Can you create simple reproducible test repo?

pdehne commented 3 years ago

Sure. Here is a test repo: https://github.com/pdehne/node-loader-test

To reproduce please open a console in the project folder of the repo and run:

npm install
npm run configureaddon
npm run buildaddon
npm run watch

Then open a second console in the main project folder and run: npm run start:watch

You will see the error message in the Electron console window: node-loader-1 0 2-error

Stop both npm processes using CTRL-c then change node-loader versions:

npm remove node-loader
npm install node-loader@0.6.0 --save-dev

Start watch / start:watch in their respective consoles again. The native node module is loaded successfully and you will see "Hello World from Node.js" in the Electron console: node-loader-0 6 0-success

alexander-akait commented 3 years ago

Weird, using:

npm remove node-loader
npm install node-loader@0.6.0 --save-dev

still have the problem:

Uncaught Error: Cannot open /path/to/node-loader-testazazaq/build/Release/nodeloadertest.node: Error: Module did not self-register: '/home/evilebottnawi/IdeaProjects/node-loader-testazazaq/build/Release/nodeloadertest.node'.

Maybe something buggy with cpp code?

pdehne commented 3 years ago

Did you run npm run configureaddon / npm run buildaddon? The repo targets Electron 10.1.1 and will only build the addon using the right target version if you use these commands.

alexander-akait commented 3 years ago

Did you run npm run configureaddon / npm run buildaddon?

Yes

Can you try other version of Electron or node?

pdehne commented 3 years ago

I upgraded the repo to Electron 11.3.0. I also streamlined the installation process to show first that it is working with 0.6.0. Please clone the repo into a new folder then run:

npm install
npm run watch

Open a second console and run

npm run start:watch

You should see "Hello World from Node.js" in the Electron console, just like in the screenshot above. If this works please first abort the two npm commands. Then, in one of the consoles run:

npm remove node-loader
npm install node-loader@latest

And again run npm run watch / npm run start:watch in the two consoles. You should see the error message from the screenshot above.

Can you reproduce?

alexander-akait commented 3 years ago

Thanks, I will look at this in near future

alexander-akait commented 3 years ago

Problem here: https://github.com/webpack-contrib/node-loader/blob/master/src/index.js#L32:

We contact them so you just have invalid path. But here two weird things - __dirname and publicPath, maybe we can generate relative paths to module.

Just interesting why you need this loader, because you can replace this loader using:

try {
  process.dlopen(module, path.resolve(__dirname, './path/to/file.ext'));
} catch (error) {
   // logging
}

To be honestly we should deprecate it in favor simple code above

vjpr commented 3 years ago

__dirname seems to be set incorrectly. There are some workarounds here but I couldn't get them to work reliably.