PawanOsman / GoogleBard

GoogleBard - A reverse engineered API for Google Bard chatbot for NodeJS
https://bard.google.com
MIT License
415 stars 57 forks source link

Error when import {Bard} is ts file #20

Closed thititongumpun closed 1 year ago

thititongumpun commented 1 year ago

E:\lab\node\line-chatgpt\node_modules\ts-node\dist\index.js:851 return old(m, filename); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module E:\lab\node\line-chatgpt\node_modules\googlebard\dist\index.js from E:\lab\node\line-chatgpt\src\index.ts not supported. Instead change the require of index.js in E:\lab\node\line-chatgpt\src\index.ts to a dynamic import() which is available in all CommonJS modules. at Object.require.extensions. [as .js] (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\index.js:851:20) at Object. (E:\lab\node\line-chatgpt\src\index.ts:21:18) at Module.m._compile (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\index.js:857:29) at Object.require.extensions. [as .ts] (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\index.js:859:16) at phase4 (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\bin.js:466:20) at bootstrap (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\bin.js:54:12) at main (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\bin.js:33:12) at Object. (E:\lab\node\line-chatgpt\node_modules\ts-node\dist\bin.js:579:5) { code: 'ERR_REQUIRE_ESM' }

thititongumpun commented 1 year ago

I used in ts also import like this

const { Bard } = await import("googlebard")

So i'm workaround

const { Bard } = await (eval('import("googlebard")') as Promise<typeof import('googlebard')>)

its works any suggestions?

ในวันที่ พฤ. 25 พ.ค. 2023 เวลา 21:55 Mehul Srivastava < @.***> เขียนว่า:

GoogleBard does not support CommonJS as of now so you cannot use require(). Instead you need to use ESM import to use it in your file as mentioned by the error logs:require() of ES Module....not supported and Instead change the require of index.js in E:\lab\node\line-chatgpt\src\index.ts to a dynamic import() which is available in all CommonJS modules.

I think you might be doing this -

const bard = require('googlebard')

Instead try this -

const { Bard } = await import("googlebard")

Can you show me the source code in case this isn't the problem you're facing?

— Reply to this email directly, view it on GitHub https://github.com/PawanOsman/GoogleBard/issues/20#issuecomment-1563054734, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANGAGP2LURFNZENVMRUNUQDXH5XHPANCNFSM6AAAAAAYNNHIGE . You are receiving this because you authored the thread.Message ID: @.***>

mehul-srivastava commented 1 year ago

So is it working or not?

thititongumpun commented 1 year ago

still same error. its only work as my import.

TakasiVenkataSandeep-08 commented 1 year ago

@thititongumpun can you try importing like this import { Bard } from "googlebard"; it is working fine for me;

mehul-srivastava commented 1 year ago

@thititongumpun can you try importing like this import { Bard } from "googlebard"; it is working fine for me;

I think he wants to use other CommonJS modules as well. That is what the error logs say. In that case, using this wont work unless the type has been set to module in the package.json file. But that would result in the code not working since he might be using other modules as well. But if that is not the case then this should be the right way to do it.

Otherwise I think his error is resolved because that is what he said in the comment above.

thititongumpun commented 1 year ago

Cant use type: module in package.json. It will throw cant solve ts file. I used only express in typescript. AS .js import { Bard } from "googlebard" it is working fine for me.

tockawaffle commented 1 year ago

You can use a dynamic import, if import("..") doesn't work for you:

const importDynamic = new Function('modulePath', 'return import(modulePath)')

And then use it in a async function, like so:

async function main() {
    const importDynamic = new Function('modulePath', 'return import(modulePath)')
    const {Bard} = await importDynamic("googlebard")
}