0

trying to run https://github.com/airgram/airgram

From this post, (node:9374) Warning: To load an ES module, set "type": "module"

already add

{"type": "module"}

To package.json

Here is my code:

root@faka:~/airgram# cat face.js 
import { Airgram, Auth, prompt, toObject } from 'airgram'

const airgram = new Airgram({
  apiId: process.env.APP_ID,
  apiHash: process.env.APP_HASH
})


airgram.use(new Auth({
  code: () => prompt(`Please enter the secret code:\n`),
  phoneNumber: () => prompt(`Please enter your phone number:\n`)
}))

void (async () => {
  const me = toObject(await airgram.api.getMe())
  console.log(`[me]`, me)
})

// Getting all updates
airgram.use((ctx, next) => {
  if ('update' in ctx) {
    console.log(`[all updates][${ctx._}]`, JSON.stringify(ctx.update))
  }
  return next()
})

// Getting new messages
airgram.on('updateNewMessage', async ({ update }, next) => {
  const { message } = update
  console.log('[new message]', message)
  return next()
})

But still got error:

root@faka:~/airgram# node face.js 
internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'airgram' imported from /root/airgram/face.js
    at packageResolve (internal/modules/esm/resolve.js:664:9)
    at moduleResolve (internal/modules/esm/resolve.js:705:18)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:798:11)
    at Loader.resolve (internal/modules/esm/loader.js:100:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:246:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
    at link (internal/modules/esm/module_job.js:46:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
mmmm
  • 81
  • 7
  • This runtime issue is not caused by typescript. And typescript _is_ a superset of javascript. – Terry Dec 04 '22 at 17:31
  • How did you install the package? It's not being found and node is looking in a weird place. It should have just been `npm install airgram`. – Aurast Dec 04 '22 at 17:33

1 Answers1

1

Your code have many errors when running it first make sure you are install airgram package:

npm install airgram

Second error

 internalBinding('errors').triggerUncaughtException(

you can view this answer to solve it.

Eng_Farghly
  • 1,987
  • 1
  • 23
  • 34
  • with your answer, I actually created a javaScript file not typeScript. Does that mean tsc fileName.ts is not necessary ? ( since my file name is fileName.js) – mmmm Dec 04 '22 at 17:41
  • I updated the answer, solve the errors `Cannot find package 'airgram'` by installing `airgram` package. After fixing it let's show code have an errors again or not. – Eng_Farghly Dec 04 '22 at 17:51
  • import { Airgram, Auth, prompt, toObject } from 'airgram' ^^^^^^ SyntaxError: Cannot use import statement outside a module at wrapSafe (internal/modules/cjs/loader.js:915:16) at Module._compile (internal/modules/cjs/loader.js:963:27) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10) at Module.load (internal/modules/cjs/loader.js:863:32) at Function.Module._load (internal/modules/cjs/loader.js:708:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) at internal/main/run_main_module.js:17:47 – mmmm Dec 04 '22 at 17:55
  • To solve this error, view these answers https://stackoverflow.com/q/58384179/5661396 , What node version that you use. – Eng_Farghly Dec 04 '22 at 17:58
  • node -v : v12.22.9. AFTER adding { "type": "module" } to package.json. RUN node face.js internal/modules/cjs/loader.js:258 throw e; ^ SyntaxError: Error parsing /home/ubuntu/airgram/package.json: Unexpected token { in JSON at position 1561 at parse () at readPackage (internal/modules/cjs/loader.js:245:20) (internal/modules/run_main.js:55:24) at internal/main/run_main_module.js:17:47 { path: '/home/ubuntu/airgram/package.json' } – mmmm Dec 04 '22 at 18:20
  • Please, upgrade to version 18.0.0 and let show me errors. – Eng_Farghly Dec 04 '22 at 19:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250151/discussion-between-eng-farghly-and-mmmm). – Eng_Farghly Dec 04 '22 at 21:45