node-telnet-client icon indicating copy to clipboard operation
node-telnet-client copied to clipboard

Error in example with async/await

Open Viiprogrammer opened this issue 2 years ago • 4 comments

Missing ; after const { Telnet } = require('telnet-client')

Error:

TypeError: require(...) is not a function
    at Object.<anonymous> (/home/righthaze/Tests/tel/index.js:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Viiprogrammer avatar Apr 28 '22 13:04 Viiprogrammer

Welcome to Node.js v14.19.1.
Type ".help" for more information.
> const { Telnet } = require('telnet-client')
undefined
> console.log(Telnet)
[class Telnet extends EventEmitter]
undefined

works for me on node.js 14, 16 and 18.

Missing ;? What are you trying to accomplish?

mkozjak avatar Apr 28 '22 13:04 mkozjak

When i run

'use strict'

const { Telnet } = require('telnet-client')

(async function () {
  const connection = new Telnet()

  // these parameters are just examples and most probably won't work for your use-case.
  const params = {
    host: '127.0.0.1',
    port: 23,
    shellPrompt: '/ # ', // or negotiationMandatory: false
    timeout: 1500
  }

  try {
    await connection.connect(params)
  } catch (error) {
    // handle the throw (timeout)
  }

  const res = await connection.exec('uptime')
  console.log('async result:', res)
})()

from example in README.md, I get error:

TypeError: require(...) is not a function
    at Object.<anonymous> (/home/righthaze/Tests/tel/index.js:5:1)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Because there is no ; before the const { Telnet } = require('telnet-client')

изображение

Viiprogrammer avatar Apr 28 '22 13:04 Viiprogrammer

; doesn't have anything to do with strict mode, as far as I'm aware. Using Node.js 18, this works on my end:

mkozjak@mbp:/tmp/a > cat a.js
'use strict'

const { Telnet } = require('telnet-client')

console.log(Telnet)
mkozjak@mbp:/tmp/a > node a.js
[class Telnet extends EventEmitter]
mkozjak@mbp:/tmp/a >

Why doesn't this code break for me?

mkozjak avatar Apr 28 '22 13:04 mkozjak

I don't know exactly why this is happening, but in node before (async() => {})() should always be a semicolon, I made a video: https://www.youtube.com/watch?v=ScjAT9dO-Ec

Viiprogrammer avatar Apr 28 '22 14:04 Viiprogrammer

this is known as IIFE, you need to semi colon to break that line, else the JS will interpret it as a function

require("....")(...)(...)

like what it is written in the error message

reference

  • https://thedavidbarton.github.io/blog/anonymous-IIFE-fn/

sweetim avatar Aug 19 '22 12:08 sweetim

Good catch, guys! Thanks!

mkozjak avatar Aug 19 '22 15:08 mkozjak