SBoudrias/Inquirer.js

select failing when arrow keys pressed cjs/lib/key.js

MattReimer opened this issue · 2 comments

I'm having trouble with the select function. When I try to use the arrow keys on my keyboard I'm getting:

TypeError: Cannot read property 'name' of undefined

here's my script:

import { select } from '@inquirer/prompts'

async function runnerMain(): Promise<void> {
  const engine = await select({
    message: `Which job do you want to run?`,
    choices: Object.values(['option1', 'option2', 'option3']).map((v) => ({
      value: v,
      name: v,
    })),
  })
  console.log(engine)
}

if (require.main === module) {
  runnerMain().catch((err) => {
    process.exit(1)
  })
}

here's the output including the error when I use the arrow keys to try to select an option

Debugger attached.
? Which job do you want to run? (Use arrow keys)
❯ option1
  option2
  option3/some/path/node_modules/@inquirer/core/dist/cjs/lib/key.js:26
const isEnterKey = (key) => key.name === 'enter' || key.name === 'return';
                                ^
TypeError: Cannot read property 'name' of undefined
    at isEnterKey (/some/path/node_modules/@inquirer/core/dist/cjs/lib/key.js:26:33)
    at Object.current (/some/path/node_modules/@inquirer/select/dist/cjs/index.js:45:35)
    at /some/path/node_modules/@inquirer/core/dist/cjs/lib/use-keypress.js:12:20
    at wrapped (/some/path/node_modules/@inquirer/core/dist/cjs/lib/hook-engine.js:46:29)
    at AsyncResource.runInAsyncScope (async_hooks.js:197:9)
    at ReadStream.emit (events.js:412:35)
    at ReadStream.emit (domain.js:475:12)
    at emitKeys (internal/readline/utils.js:358:14)
    at emitKeys.next (<anonymous>)
    at ReadStream.onData (readline.js:1270:36)

Waiting for the debugger to disconnect...

Relevant context

  • I'm on OSX running the script in VSCode using launch.json
  • I've tried wiping node_modules and re-installing.
> node --version
v18.16.0

something weird I noticed

> npm list @inquirer/core
@mynamespace/mypackage@0.0.1 /some/path
└─┬ @mynamespace/mypackage@0.0.23 -> ./packages/localdev
  └─┬ @inquirer/prompts@2.3.1
    ├─┬ @inquirer/checkbox@1.4.0
    │ └── @inquirer/core@5.1.0
    ├─┬ @inquirer/confirm@2.0.14
    │ └── @inquirer/core@5.1.0 deduped
    ├── @inquirer/core@2.3.1
    ├─┬ @inquirer/editor@1.2.12
    │ └── @inquirer/core@5.1.0 deduped
    ├─┬ @inquirer/expand@1.1.13
    │ └── @inquirer/core@5.1.0 deduped
    ├─┬ @inquirer/input@1.2.13
    │ └── @inquirer/core@5.1.0 deduped
    ├─┬ @inquirer/rawlist@1.2.13
    │ └── @inquirer/core@5.1.0 deduped
    └─┬ @inquirer/select@1.3.0
      └── @inquirer/core@5.1.0 deduped

notice how there's a version of @inquirer/core@2.3.1. Could this be causing problems?

As a followup I did try:

  "resolutions": {
    "@inquirer/core": "5.1.0"
  },

but it did not fix the issue.

notice how there's a version of @inquirer/core@2.3.1. Could this be causing problems?

No that shouldn't be the cause... That same bug was reported in #1312, but it was likely due to an old version of Node (I would recommend logging the node version in your script to make sure it matches your expectation).

I cannot reproduce (and it's weird, the keypress event would be undefined for some reason.) So I'm missing information as to the environment this happens into; is there anything different you're doing? Does it fails outside of vscode? Does it fails without the debugger attached? And does it fail with latest @inquirer/prompts (you're one major behind)?

Yeah. I checked again and it turns out this was a red herring. You were 100% right and my VSCode was not choosing the correct version even though my terminal and .nvmrc all had the right one.

once I added "runtimeExecutable": "${env:NVM_BIN}/node", everything started working again.

Thanks for jumping in on this and sorry for the misdirect. I should have caught the problem first. 😝