webpack-contrib / node-loader

node loader for native modules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keytar

opened this issue · comments

Hi

A short background: I develop in Quasar for the web and then build it in electron mode to run as an executable. Everything is working fine in this regard. Now I want to add something specific only when running in electron mode.

To implement that feature, I need keytar. I npm installed it put things in place (I hope) but when running the application as web mode I see the following in the console:

Cannot open C:\Users\user\Downloads\portable\vscode\Projects\project\node_modules\keytar\build\Release\keytar.node: TypeError: Cannot read property 'dlopen' of undefined
at Object.eval (keytar.node?3002:1)

and the line itself is

try {global.process.dlopen(module, "C:\Users\user\Downloads\portable\vscode\Projects\project\node_modules\keytar\build\Release\keytar.node"); } catch(e) {throw new Error('Cannot open ' + "C:\Users\user\Downloads\portable\vscode\Projects\project\node_modules\keytar\build\Release\keytar.node" + ': ' + e);}
Obviously global.process is undefined.

Any idea why global.process is undefined?
I put this in conf.js file

            extendWebpack(cfg) {
                cfg.module.rules.push({
                    enforce: "pre",
                    test: /.node$/,
                    loader: "node-loader",
                });

Please use the issue template int he future, your environment doesn't support global process, so you get this error, you can create reproducible test repo and I will investigate it

Apologies.

I created a new quasar project, installed keytar and node-loader.
It's enough to import keytar (see mainlayout.vue file in layouts folder) to make this problem (without it, the project opens up).
Thanks
https://gofile.io/d/HDsLoV

Here interesting case, global is undefined in electron env, we can rewrite it to process.dlopen, but it is still unsafe to do because https://www.electronjs.org/docs/tutorial/multithreading#native-nodejs-modules. My recommendation is avoid using native Node.js modules in the electron env

Also I can't build your app due 2 errors, try to load your project and build it

Thanks. This part I'm less familiar with.

Quasar (based on vue) is the environment and I "tell' it to build it in electron mode. One of my questions was whether the problem is because this is not electron mode but I'm not sure how to test this guess.

I run the project using quasar dev please try it and let me know. It is running but fails immediately because global.process is undefined, I hope you'll see it on your side too.

A bit more information. I tried to do the following:

try {
    let keyt = require("keytar");
} catch (e) {
    this.errorMessage = e;
    this.showError = true;
}

When running as "web", I got the same error as before with global.process undefined.
When running as "electron", I got the following:

Error: node-loader: Error: C:\Users\user\AppData\Local\Temp\e6425528-a110-43aa-a474-41723fc46383.tmp.node is not a valid Win32 application. C:\Users\user\AppData\Local\Temp\e6425528-a110-43aa-a474-41723fc46383.tmp.node

Building as electron generates a portable executable that I run. My guess is that when running, it extracts the inside to the temp folder.

Looks is not compatibility with win32, I can't fix it, sorry, global.process is undefined for web, because it is node-loader, only for node

global.process is undefined when in desktop mode: that's obvious so there is nothing to fix here.
Error when in electron mode: The failure is on this line: let keyt = require("keytar"); Is there anything else I can add/check in order to provide more details?

Error when in electron mode: The failure is on this line: let keyt = require("keytar"); Is there anything else I can add/check in order to provide more details?

Try to load this file in app without node-loader

The application is running and working, only that line is causing the error I posted here.
It could be something I did wrong (I posted the code here), if there's more I can do to give more information, let me know.

@amosss How you include the module above?

Can you modify loader code before run and change global.process.dlopen to process.dlopen?

Assuming I understood you correctly, I changed global.process.dlopen to process.dlopen in index.js and it actually made some progress.

It passed the failed code from before but now something weird is happening
image
Even though it seems to recognized the relevant function getPassword still it fails with the error from the screenshot.

I changed global.process.dlopen to process.dlopen in index.js and it actually made some progress.

Yes, look we should change it to process.dlopen, I will send a PR

Even though it seems to recognized the relevant function getPassword still it fails with the error from the screenshot.

Unfortunately, I won't help you here. It can be problem with your code or library, or other problems. node-loader just load native module

My code is as simple as

this.keyt = require("keytar");
if (this.keyt) {
    this.keyt.getPassword("test", "email").then((result) => {
        console.log("result: " + result);
    });
}

From the screenshot above, you see that keyt is recognized with its functions but this.keyt.getPassword still fails for the mentioned error. Is my code that bad? :) Any tip to get me going forward?
Thanks

From the screenshot above, you see that keyt is recognized with its functions but this.keyt.getPassword still fails for the mentioned error. Is my code that bad? :) Any tip to get me going forward?

I am really don't know, try to Object.keys(this.keyt)