oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.31k stars 2.78k forks source link

node-telegram-bot-api not not work with bun - error Requested module is not instantiated yet #3854

Closed davidtranjs closed 1 year ago

davidtranjs commented 1 year ago

What version of Bun is running?

0.7.0

What platform is your computer?

Darwin 22.5.0 arm64 arm

What steps can reproduce the bug?

package.json

{
  "name": "tele-backup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "archiver": "^5.3.1",
    "dayjs": "^1.11.9",
    "node-telegram-bot-api": "^0.61.0"
  }
}

run bun install

index.js

const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
const TelegramBot = require("node-telegram-bot-api");
const dayjs = require("dayjs");

// fake BOT_TOKEN and BACKUP_LOG_CHAT_ID
const BOT_TOKEN = "XYZ";
const BACKUP_LOG_CHAT_ID = 123;

const bot = new TelegramBot(BOT_TOKEN, { polling: false });

const folderPath = path.join("./images");

const zipFileName = `images_${dayjs().format("DD_MM_YYYY")}.zip`;
const zipPath = path.join("./images", zipFileName);
const output = fs.createWriteStream(zipPath);
const totalFileCount = fs.readdirSync(folderPath).length;

console.log(">>> Zip file path: ", zipPath);
console.log(">>> Total file count: ", totalFileCount);

const archive = archiver("zip", {
  zlib: { level: 9 }, // Set the compression level to maximum
});

output.on("error", (error) => {
  console.log(`>>> Error writing zip file: ${error}`);
});

archive.on("end", () => {
  console.log(`>>> Zip file ${zipFileName} created successfully`);

  const stream = fs.createReadStream(zipPath);

  bot
    .sendDocument(
      BACKUP_LOG_CHAT_ID,
      stream,
      {},
      {
        filename: zipFileName,
      }
    )
    .then(() => {
      const fileSize = (fs.statSync(zipPath).size / 1024 / 1024).toFixed(2);
      console.log(`>>> Zip file ${fileSize}MB sent to Telegram group`);

      fs.unlink(zipPath, (err) => {
        if (err) {
          console.error(err);
          return;
        }
        console.log(">>> Zip file removed");
      });
    })
    .catch((error) => {
      console.log(`>>> Error sending zip file to Telegram group: ${error}`);
    });
});

archive.pipe(output);
archive.directory(folderPath, false);
archive.finalize();

Run bun index.js

What is the expected behavior?

Run ok with node 16.20.0

>>> Zip file path:  images/images_28_07_2023.zip
>>> Total file count:  6
>>> Zip file images_28_07_2023.zip created successfully
(node:80966) [node-telegram-bot-api] DeprecationWarning: In the future, content-type of files you send will default to "application/octet-stream". See https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files for more information on how sending files has been improved and on how to disable this deprecation message altogether.
(Use `node --trace-deprecation ...` to show where the warning was created)
>>> Zip file 0.14MB sent to Telegram group
>>> Zip file removed

What do you see instead?

It show error when run with bun index.js

>>> Zip file path:  images/images_28_07_2023.zip
>>> Total file count:  6
TypeError: Requested module is not instantiated yet.

Additional information

No response

davidtranjs commented 1 year ago

Update: 0.7.1 still having this issue

rubnogueira commented 1 year ago

@Jarred-Sumner this also happens with request-promise in 0.7.3. Simple repro:

import requestPromise from 'request-promise';

birkskyum commented 1 year ago

@dungmidside , can you run that script with Node >= 18 ? Given the upcomming node 16 sunset.

davidtranjs commented 1 year ago

@birkskyum I ran my script with node 18.17.1 and it show the same result with node 16 I ran it again with latest bun 0.8.1 and it still throw the same error

Jarred-Sumner commented 1 year ago

This error comes from JavaScriptCore's ES module loader. It is thrown here: https://github.com/oven-sh/WebKit/blob/main/Source/JavaScriptCore/builtins/ModuleLoader.js#L511

Not sure why this happens yet. It's likely an exception being thrown and then some other code missing an exception check.

davidtranjs commented 1 year ago

Hi @Jarred-Sumner @birkskyum, is their any plan for fixing this issue ?

TimHeckel commented 1 year ago

FWIW, I'm seeing this thrown as well in two different scenarios: when attempting to instantiate the SecretsManager class using the AWS SDK v2 (^2.1046.0), or when attempting to connect to mongo (^5.1.0) - two quick encounters as I tried bun on our large nodejs app. I'm running node v16.15.0

const mongoClient = require("mongodb").MongoClient;
const client = new mongoClient(myUrl);
await client.connect(); <-- Requested module is not instantiated yet exception thrown in bun
const AWS = require("aws-sdk");
// AWS is configured ....
const secretsManager = new AWS.SecretsManager(); <-- Requested module is not instantiated yet exception thrown in bun
yeung66 commented 1 year ago

same problem happens to my project. error Requested module is not instantiated yet on using node-telegram-bot-api, but work well on nodejs

adefirmanf commented 1 year ago

Following up on the answer from @rubnogueira , I think the dependency that used request-promise should return this error. node-telegram-bot-api also uses request-promise

import request from 'request-promise';
request;

TypeError: Requested module is not instantiated yet.
      at processTicksAndRejections (:1:2602)

Since there's a lot of dependency being used by this library, I think this bug needs to be fixed ASAP.

tomsjansons commented 1 year ago

we are experiencing this problem as well. request-promise and/or request-promise-native seems to be the cause for this problem. We've got a fairly large and old project where specific packages or even versions are unavoidable without a fairly large workaround unfortunately

melroy89 commented 1 year ago

I think node telegram bot api needs to be move away from the deprecated request-promise library asap! Solving a lot of issues, incl. Bun but also security issues with this deprecated/not maintained package.

rubnogueira commented 1 year ago

I think node telegram bot api needs to be move away from the deprecated request-promise library asap! Solving a lot of issues, incl. Bun but also security issues with this deprecated/not maintained package.

That is not a solution. According to request-promise github, the package is used by almost 300k public projects, so it means that at least 300k projects don't work with bun (our codebase also uses that lib).

https://github.com/request/request-promise/network/dependents

image

melroy89 commented 1 year ago

Also update your code base 🀣. And move away from this library.

Hehe I get it. I understand it's better to resolve the issue in Bun as well.

rafsawicki commented 1 year ago

I had the same issue with my personal project with versions 0.8.1 and 1.0.0, but it seems to be fixed in 1.0.2. I haven't seen anything explicitly mentioning this bug in changelogs, but it's worth retesting with the latest version.

rubnogueira commented 1 year ago

I had the same issue with my personal project with versions 0.8.1 and 1.0.0, but it seems to be fixed in 1.0.2. I haven't seen anything explicitly mentioning this bug in changelogs, but it's worth retesting with the latest version.

I confirm, it is fixed in 1.0.2 :)

adefirmanf commented 1 year ago

Yupp, it was fixed in 1.0.2 πŸŽ‰

image

I think we can close this issue @dungmidside @Jarred-Sumner

davidtranjs commented 1 year ago

Confirmed this issue is fixed in 1.0.2 Thanks @Jarred-Sumner and bun's team

artemszelenov commented 3 months ago

I have the same issue with bun 1.1.20 πŸ˜„

pitzzahh commented 2 months ago

Same here

PS C:\Users\Student\hrms> bun db:push
$ drizzle-kit push
[bun] Warning: async_hooks.createHook is not implemented in Bun. Hooks can still be created but will never be called.
No config path provided, using default 'drizzle.config.ts'
Reading config file 'C:\Users\Student\hrms\drizzle.config.ts'
Using 'pg' driver for database querying
[βœ“] Pulling schema from database...
[i] No changes detected
PS C:\Users\Student\hrms> bun -v
1.1.26
TibixDev commented 1 month ago

Almost the same exact issue in my case as @pitzzahh

❯ bun run db:generate
$ drizzle-kit generate
[bun] Warning: async_hooks.createHook is not implemented in Bun. Hooks can still be created but will never be called.
No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/tibix/fastsrc/proj/backend/drizzle.config.ts'
0 tables

No schema changes, nothing to migrate 😴
davidtranjs commented 1 month ago

@pitzzahh @TibixDev your issues seem not relate to error Requested module is not instantiated yet at all. Can you share more detail ?