Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pdehne opened this issue Feb 28, 2021 · 9 comments
Open

Comments

@pdehne
Copy link

pdehne commented Feb 28, 2021

  • Operating System: Windows
  • Node Version: 12.6.3
  • NPM Version: 7.6.0
  • webpack Version: 5.24.2
  • node-loader Version: 1.0.2

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
Copy link
Member

Can you create simple reproducible test repo?

@pdehne
Copy link
Author

pdehne commented Mar 2, 2021

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 [email protected] --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
Copy link
Member

Weird, using:

npm remove node-loader
npm install [email protected] --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
Copy link
Author

pdehne commented Mar 3, 2021

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
Copy link
Member

Did you run npm run configureaddon / npm run buildaddon?

Yes

Can you try other version of Electron or node?

@pdehne
Copy link
Author

pdehne commented Mar 3, 2021

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
Copy link
Member

Thanks, I will look at this in near future

@alexander-akait
Copy link
Member

alexander-akait commented Mar 11, 2021

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

  • __dirname is /path/to/node-loader/node_modules/electron/dist/resources/electron.asar/renderer
  • publicPath is http://localhost:8084/
  • name is build/Release/nodeloadertest.node

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
Copy link

vjpr commented Nov 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants