waitingsong / node-win32-api

win32 api
MIT License
430 stars 55 forks source link

Cannot find module 'node: assert' Require stack #43

Open chaklasiyanikunj opened 1 year ago

chaklasiyanikunj commented 1 year ago

I installed win32-API version => "win32-api": "^20.1.0". My Node version is 14.18.5.

The Notepad file is open on my PC.

Screenshot 2022-11-24 163247

try {
          console.log("Preload -- Try part called")
          const Win32 = require('win32-api/promise');
          const User32 = Win32.User32.load(['FindWindowExW', 'SetWindowPos']);
          var className = "Notepad"
          const SWP_NOMOVE = 0x0002, SWP_NOACTIVATE = 0x0010;

          module.exports = {
               ResizeWindow: async function ResizeWindow(className) {
                    const lpszClass = Buffer.from(className + '\0', 'ucs2')
                    const hWnd = await User32.FindWindowExW(0, 0, lpszClass, null); 
                    User32.SetWindowPos(hWnd, 0, 0, 0, 600, 600, SWP_NOMOVE | SWP_NOACTIVATE); 
               }
          };
} catch(e){
       console.log("Preload -- Catch part called")
       console.log("Exception ::",e)
}

Screenshot 2022-11-24 162636

When trying this code in electron it gives this => "Cannot find module 'node: assert' Require stack" error.

Screenshot 2022-11-24 162702

I've tried to find the solution. but haven't found anything yet. Anyone any idea about this?

waitingsong commented 1 year ago

Node v14 not support syntax import * from 'node:assert' , only import * from 'assert' without node: prefix Try upgrade the version to v16?

chaklasiyanikunj commented 1 year ago

Thank you for your reply. I already tried with Node versions: 16.17.0 and 16.17.1. But received the same error. I wrote only the above code. Do we need to require any other statement?

waitingsong commented 1 year ago

whats the version of electron?

chaklasiyanikunj commented 1 year ago

I used the below electron version :

"electron": "^11.3.0",
"electron-local-crash-reporter": "^1.0.2",
"electron-progressbar": "^2.0.0",
"electron-updater": "^4.3.9",
waitingsong commented 1 year ago

The version of nodejs may under v14 some where. You can try a simple file:

const assert = requrire('node:assert')
console.log(assert)

or

import * as assert from 'node:assert'
console.log(assert)

v14 also suppports https://nodejs.org/api/esm.html#node-imports

chaklasiyanikunj commented 1 year ago

I tried both statements. Still, I am getting this error. Do we need to add this npm package to Electron App?

waitingsong commented 1 year ago

I think https://www.npmjs.com/package/assert is unnecessary, it's not nodejs inernal module ( import by prfix node:) . provide a simple repo for debug ?

chaklasiyanikunj commented 1 year ago

Sample Repo is available here and I added a Readme.txt file for more details. Electron_Sample_App.zip

waitingsong commented 1 year ago

electron v11 bundled with nodejs v12 : https://www.electronjs.org/blog/electron-11-0 electron v12 bundled with nodejs v14, pls try

Jugbot commented 1 year ago

I found that you need to add this to your BrowserWindow:

    webPreferences: {
      sandbox: false,
      //nodeIntegration: true, // Or this
    }

Otherwise node libraries wont be available. Note that this is done for security reasons.

Tangential note (unrelated to the above issue) electronV21+ wont work due to https://www.electronjs.org/blog/v8-memory-cage