Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
barrynorman opened this issue Jul 16, 2023 · 6 comments
Closed

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

barrynorman opened this issue Jul 16, 2023 · 6 comments

Comments

@barrynorman
Copy link

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
Copy link
Collaborator

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
Copy link
Owner

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

@barrynorman
Copy link
Author

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

@gelevanog
Copy link

gelevanog commented Jul 22, 2023

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
Copy link

@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
Copy link

I used NestJS and my os is ubuntu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants