nut-tree / nut.js

Native UI testing / controlling with node
https://nutjs.dev
2.28k stars 130 forks source link

Error with Nextron: TypeError: Cannot read properties of undefined (reading 'indexOf') #473

Closed jzellis closed 1 year ago

jzellis commented 1 year ago

Hello! I'm trying to use Nut with Nextron (Electron + Next.js), but I'm getting the following error whenever I attempt to include the keyboard.type function in my code:

TypeError: Cannot read properties of undefined (reading 'indexOf')

Call Stack
Function.getFileName
../node_modules/bindings/bindings.js (178:0)
bindings
../node_modules/bindings/bindings.js (82:0)
eval
../node_modules/@nut-tree/libnut-darwin/permissionCheck.js (1:33)
../node_modules/@nut-tree/libnut-darwin/permissionCheck.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (532:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/libnut-darwin/index.js (3:13)
../node_modules/@nut-tree/libnut-darwin/index.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (522:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/libnut/index.js (8:10)
eval
../node_modules/@nut-tree/libnut/index.js (10:1)
../node_modules/@nut-tree/libnut/index.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (582:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-mouse.class.js (3:15)
../node_modules/@nut-tree/nut-js/dist/lib/provider/native/libnut-mouse.class.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (977:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/nut-js/dist/lib/provider/provider-registry.class.js (7:45)
../node_modules/@nut-tree/nut-js/dist/lib/provider/provider-registry.class.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (1010:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/nut-js/dist/lib/sleep.function.js (7:50)
../node_modules/@nut-tree/nut-js/dist/lib/sleep.function.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (1065:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/nut-js/dist/lib/keyboard.class.js (5:25)
../node_modules/@nut-tree/nut-js/dist/lib/keyboard.class.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (757:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/@nut-tree/nut-js/dist/index.js (25:25)
../node_modules/@nut-tree/nut-js/dist/index.js
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (603:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
webpack-internal:///./pages/home.tsx (16:74)
./pages/home.tsx
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/pages/home.js (3073:1)
options.factory
/_next/static/chunks/webpack.js (647:31)
__webpack_require__
file:///Users/jzellis/Projects/midipadconfig/renderer/.next/static/chunks/webpack.js (37:33)
fn
/_next/static/chunks/webpack.js (302:21)
eval
../node_modules/next/dist/build/webpack/loaders/next-client-pages-loader.js?absolutePagePath=%2FUsers%2Fjzellis%2FProjects%2Fmidipadconfig%2Frenderer%2Fpages%2Fhome.tsx&page=%2Fhome! (5:15)
eval
../node_modules/next/dist/client/route-loader.js (211:50)

The odd part is that I don't get it if I just do import { keyboard } from '@nut-tree/nut-js'; -- it's only if I actually include the keyboard.type function in my code. Even weirder is that it errors out on startup, not when the function would be called.

I'm a newbie to Electron development in general, so maybe I'm missing something obvious. I tried searching for this and found #250 , but that's using Vue and Vue's config files.

Anybody have any idea where I might start on fixing this? I'm building a little app that lets me assign custom key commands to MIDI events coming from a macropad I've built, and Nut.js seems to be the best (possibly only) solution for doing this in Electron.

Thanks!

s1hofmann commented 1 year ago

Hey @jzellis 👋

can you link a repo so I can check out your project? I don’t know this setup so I’d have to check that out first.

Best regards

Simon

esteban-url commented 1 year ago

hey @s1hofmann i have the same problem. here is a repo: https://github.com/esteban-url/nextron-nutjs

very straight forward. the mouse does move but the page breaks. please let me know if I can help in some other way

steps to reproduce

  1. clone repo
  2. install deps: yarn install
  3. run app: yarn dev
  4. 🚨 the mouse moves, but the error shows up
s1hofmann commented 1 year ago

Hi @esteban-url 👋

You are calling nut.js inside your render process, which should be avoided.

You can see that this has a side-effect during build, when the cursor starts bouncing. Additionally, and this is the core of your problem, nut.js is a node package, so it's not suitable to be imported in a web context.

Instead, move calls to nut.js to your main process and switch to IPC communication between both processes. See this nextron example as a starter.

Best regards

Simon

@jzellis I'm assuming you're hit by the same problem, but without a repo I can't tell 🤷‍♂️

esteban-url commented 1 year ago

thanks for pointing me in the right direction @s1hofmann. it's working now!