EvanZhouDev / bard-ai

A lightweight library to access Google Bard.
https://www.npmjs.com/package/bard-ai
GNU General Public License v3.0
117 stars 31 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module... #35

Closed barrynorman closed 1 year ago

barrynorman commented 1 year ago

Hello!

I'm trying to implement a short typescript script to play around, but somehow I am not able to get it running. Here are the most important files.

{
  "name": "bard",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.ts",
  "scripts": {
      "start": "ts-node ./src/index.ts"
  },
  "keywords": [],
  "author": "anon",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^20.4.2",
    "bard-ai": "^1.5.4",
    "ts-node": "^10.9.1"
  }
}
import { askAI, init } from "bard-ai";

const COOKIE_KEY =
  "__Secure-1PSID=XXX";

const main = async () => {
  await init(COOKIE_KEY);
  console.log(await askAI("Hello world!"));
};

main();

When I try

npm run start

i get this:

Error [ERR_REQUIRE_ESM]: require() of ES Module ~/bard/node_modules/bard-ai/index.js from ~/bard/src/index.ts not supported.
Instead change the require of index.js in ~/bard/src/index.ts to a dynamic import() which is available in all CommonJS modules.

Any idea why? I'm not sure if i understand the error.

ThatXliner commented 1 year ago

This isn't a bug of Bard, just a general JavaScript error. Try changing your start script to ts-node --esm ./src/index.ts

EvanZhouDev commented 1 year ago

Yep! bard-ai only works with modern "ESM" import syntax, as per standards these days.

barrynorman commented 1 year ago

Still not working. --esm does not help. Did one of you tried my example?

gelevanog commented 1 year ago

try this

async getBardRes(text) {
    const importDynamic = new Function(
      'modulePath',
      'return import(modulePath)',
    );
   const { init, askAI } = await importDynamic('bard-ai');
   await init(COOKIE_KEY);
   return await askAI(text);
}
tuandev1l commented 12 months ago

@gelevanog well, i tried this but it throws Error like this. I comment await init(COOKIE_KEY);, it throws error at askAI similar to init. image

tuandev1l commented 12 months ago

I used NestJS and my os is ubuntu.